serilog-sinks-elasticsearch icon indicating copy to clipboard operation
serilog-sinks-elasticsearch copied to clipboard

No exception data in elastic for logger.Error(ex, "...

Open iomdesign opened this issue 2 years ago • 1 comments

A few questions before you begin:

Is this an issue related to the Serilog core project or one of the sinks or community projects.
This issue is intended for Serilog Elasticsearch Sink issues.

Does this issue relate to a new feature or an existing bug?

  • [ x ] Bug

What version of Serilog.Sinks.Elasticsearch is affected? Please list the related NuGet package. 8.4.1

What is the target framework and operating system? See target frameworks & net standard matrix.

  • [ x ] 4.6.x

Please describe the current behavior? In the following scenario, the log in elastic has the log message but no exception whereas the same log written to file contains the full exception details.

try
{
...
}
catch (Exception ex)
{
    logger.Error(ex, "Error doing some stuff: {xyz}", xyz);
    ...do some handling stuff
}

I have noticed in a different project of the same solution (same versions of serilog packages and target framework) that the following scenario resulted in the log in elastic being as expected...

catch (Exception ex)
{
    var exMessage = ex.Message;
    logger.Error(ex, " Some error - [exMessage: {exMessage}]", exMessage);
    throw;
}

and the log entry contained

    "exceptions": [
      {
        "Depth": 0,
        "ClassName": "System.ServiceModel.FaultException",
        "Message": "Server was unable to process request. ---> Exception has been thrown by the target of an
... 

Please describe the expected behavior? Log in elastic should contain exception

If the current behavior is a bug, please provide the steps to reproduce the issue and if possible a minimal demo of the problem This is our logging config

        public static void StartLogger()
        {
            SetEnvironmentName();
            //Change TLS version to support elasticsearch
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

            Log.Logger = new LoggerConfiguration()
                .WriteTo.File(FilePattern, rollingInterval: RollingInterval.Day)
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(ElasticSearchUrl))
                {
                    AutoRegisterTemplate = false,
                    //AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                    IndexFormat = string.Format("{0}{1}", ElasticSearchIndex, "-{0:yyyy.MM.dd}"),
                    ModifyConnectionSettings = x => x.BasicAuthentication(ElasticSearchUser, ElasticSearchPassword),
                    TypeName = null
                })
                .MinimumLevel.Is(SerilogMinimumLevel)
                .Enrich.WithProperty("Application", AppName)
                .Enrich.WithProperty("RequestId", Guid.NewGuid())
                .Enrich.WithEnvironmentName()
                .Enrich.WithMachineName()
                .Enrich.FromLogContext()
                .CreateLogger();

            Log.Information("App up");
        }

iomdesign avatar Jun 01 '22 14:06 iomdesign

Take a look at this issue: https://github.com/elastic/ecs-dotnet/issues/158