elastic / ecs-dotnet

https://www.elastic.co/guide/en/ecs-logging/dotnet/current/setup.html
Apache License 2.0
120 stars 62 forks source link

[BUG] Exceptions aren't logged to target when using ElasticCommonSchema #139

Closed fabianrieder closed 3 years ago

fabianrieder commented 3 years ago

ECS integration/library project(s) (e.g. Elastic.CommonSchema.Serilog): Elastic.CommonSchema.NLog + Elastic.Apm.NLog

ECS schema version (e.g. 1.4.0):

ECS .NET assembly version (e.g. 1.4.2): 1.5.1

Elasticsearch version (if applicable):

.NET framework / OS: .NET-Framework 4.7.2, Win 10

Description of the problem, including expected versus actual behavior: If I log a Warning with an exception, the logentry isn't written to the file when i use the EcsLayout

Steps to reproduce:

  1. add Extensions to nlog.config (also Install the according NuGet-Packages):
    <extensions>
        <add assembly="NLog.Targets.ElasticSearch"/>
        <add assembly="Elastic.Apm.NLog"/>
        <add assembly="Elastic.CommonSchema.NLog"/> 
    </extensions>
  2. Add a target that uses the EcsLayout
    <target xsi:type="File" name="logEcs" fileName="./logs/ecs.json">
      <layout xsi:type="EcsLayout"/>
    </target>
  3. try to write a log-entry with an exception:

        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
    
        static void Main(string[] args)
        {
            Logger.Info("Hello world");
            Logger.Warn(new KeyNotFoundException("Key"), "Error!!!");
        }

The Output-File only has 1 logmessage written, the one with the exception is missing_

{"@timestamp":"2021-03-31T13:53:32.6042159+02:00","log.level":"Info","message":"Hello world","ecs":{"version":"1.5.0"},"event":{"severity":6,"timezone":"Mitteleurop\u00E4ische Zeit","created":"2021-03-31T13:53:32.6042159+02:00"},"host":{"name":"*********","ip":["******"]},"log":{"logger":"Logging.Elastic.Demo.NLogTest.Program","original":"Hello world"},"process":{"thread":{"id":*******},"pid":*****,"name":"Logging.Elastic.Demo.NLogTest","executable":"C:\\Source\\Logging.Elastic.Demo\\Logging.Elastic.Demo.NLogTest\\bin\\Debug\\Logging.Elastic.Demo.NLogTest.exe","title":"C:\\Source\\Logging.Elastic.Demo\\Logging.Elastic.Demo.NLogTest\\bin\\Debug\\Fronius.Logging.Elastic.Demo.NLogTest.exe"},"server":{"user":{"name":"********"}}}

The NLOG-Logfile shows the following Error/Exception:

2021-03-31 13:53:32.8682 Error File Target[logEcs]: Error has been raised. Exception: System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
   bei Elastic.CommonSchema.NLog.EcsLayout.CatchError(Exception error) in /home/mpdreamz/projects/elastic/ecs-dotnet/src/Elastic.CommonSchema.NLog/EcsLayout.cs:Zeile 191.
   bei Elastic.CommonSchema.NLog.EcsLayout.GetError(Exception exception) in /home/mpdreamz/projects/elastic/ecs-dotnet/src/Elastic.CommonSchema.NLog/EcsLayout.cs:Zeile 168.
   bei Elastic.CommonSchema.NLog.EcsLayout.RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target) in /home/mpdreamz/projects/elastic/ecs-dotnet/src/Elastic.CommonSchema.NLog/EcsLayout.cs:Zeile 122.
   bei NLog.Layouts.Layout.RenderAppendBuilder(LogEventInfo logEvent, StringBuilder target, Boolean cacheLayoutResult)
   bei NLog.Targets.FileTarget.RenderFormattedMessage(LogEventInfo logEvent, StringBuilder target)
   bei NLog.Targets.FileTarget.RenderFormattedMessageToStream(LogEventInfo logEvent, StringBuilder formatBuilder, Char[] transformBuffer, MemoryStream streamTarget)
   bei NLog.Targets.FileTarget.Write(LogEventInfo logEvent)
   bei NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)
fabianrieder commented 3 years ago

Same behaviour with Serilog when using EcsTextFormatter (as described in this documentation https://www.elastic.co/guide/en/apm/agent/dotnet/master/serilog.html#serilog

Leftyx commented 3 years ago

It seems the issue has been fixed as I have tested the source code and it works fine. Any plans to push to nuget ?

russcam commented 3 years ago

@Leftyx @fabianrieder I'm looking at pushing out a new release

russcam commented 3 years ago

@Leftyx 1.5.3 has been released to nuget. Are you able to upgrade and report if it solves the issue?

Leftyx commented 3 years ago

@russcam I've updated the package to 1.5.3 and it works like charm. Thank

russcam commented 3 years ago

Great to hear, @Leftyx! I'll close this issue now

AlexGoris-KasparSolutions commented 3 years ago

@fabianrieder did you resolve your issue with Serilog with 1.5.3 of this package? I keep having the same problem, if I remove the CustomFormatter = EcsTextFormatter() line my logs do have exceptions in them in elastic (but not all the other ECS fields), and when I add the exceptions are lost...

fabianrieder commented 3 years ago

@fabianrieder did you resolve your issue with Serilog with 1.5.3 of this package? I keep having the same problem, if I remove the CustomFormatter = EcsTextFormatter() line my logs do have exceptions in them in elastic (but not all the other ECS fields), and when I add the exceptions are lost...

Unfortunately not. i still have the same issue that exceptions aren't sent to ElasticSearch when usingn