Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message is not parsed from Log2Console when Exception is logged #45

Closed
kevinbrechbuehl opened this issue Jun 18, 2019 · 8 comments · Fixed by #46
Closed

Message is not parsed from Log2Console when Exception is logged #45

kevinbrechbuehl opened this issue Jun 18, 2019 · 8 comments · Fixed by #46
Labels

Comments

@kevinbrechbuehl
Copy link

I'm using .NET Core and have configured Serilog as follow:

Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .CreateLogger();

With appsettings.json:

"Serilog": {
    "Using": [ "Serilog.Sinks.Udp" ],
    "WriteTo": [
      {
        "Name": "Udp",
        "Args": {
          "remoteAddress": "127.0.0.1",
          "remotePort": 7071,
          "formatter": "Serilog.Sinks.Udp.TextFormatters.Log4jTextFormatter, Serilog.Sinks.Udp, Version=5.0.1.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10"
        }
      }
    ]
  }

So everything works fine and my logs appear correctly in Log2Console when I do something like that:

public class AppController : Controller
{
    private readonly ILogger<AppController> logger;

    public AppController(ILogger<AppController> logger)
    {
        this.logger = logger;
    }

    public string Test()
    {
        this.logger.LogError("This is my test error");

        return "test";
    }
}

But when I change the code to the following, then the message in Log2Console is not parsed correctly.

public string Test()
{
    try
    {
        int.Parse("lala");
    }
    catch (Exception exception)
    {
        this.logger.LogError(exception, "This is my test error");
    }

    return "test";
}

in Log2Console I see the following message, marked an Info:

CallSiteClass: 
CallSiteMethod: 
File: 
Line: 0
<log4j:event xmlns:log4j="http://jakarta.apache.org/log4j/" logger="Api.Controllers.AppController" timestamp="1560851914474" level="ERROR"><log4j:message>This is my test error</log4j:message><log4j:throwable>System.FormatException: Input string was not in a correct format.

   at System.Number.StringToNumber(ReadOnlySpan`1 str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)

   at System.Number.ParseInt32(ReadOnlySpan`1 s, NumberStyles style, NumberFormatInfo info)

   at System.Int32.Parse(String s)

   at Api.Controllers.AppController.Test() in C:\Development\Api\Controllers\AppController.cs:line 22</log4j:throwable></log4j:event>

I'm using .NET Core 2.2 with Serilog 2.1.1 and Serilog.Sinks.Udp 5.0.1.

Am I missing something on the configuration or is there something wrong in the pattern?

Thanks for looking into it and best regards,
Kevin

@FantasticFiasco
Copy link
Owner

Hi and welcome to Serilog.Sinks.Udp!

It looks like you've found a bug, I will look into this issue tonight, it seems that it should be easy to reproduce.

Thank you for reporting it!

@FantasticFiasco
Copy link
Owner

Due to your detailed description I had no problems finding the issue. I was not escaping XML characters (<, >, &) in the exception message, thus producing illegal XML.

I'm in the process of merging the fix to master, and will then start working towards a release. Thank you for reporting the issue!

FantasticFiasco added a commit that referenced this issue Jun 18, 2019
Correctly XML escape exception message serialized by `Log4jTextFormatter`.

Closes #45
@FantasticFiasco
Copy link
Owner

It will take some time for an official version with your fix to be released. If you are being blocked, I would recommend you to copy the code found in Log4jTextFormatter, renaming the class to MyLog4jTextFormatter or something similar and remove the class once the new official version has been released.

@kevinbrechbuehl
Copy link
Author

Thanks for the very fast fix and the proposed workaround until the new release. Glad I could help :)

@FantasticFiasco
Copy link
Owner

There is a new pre-release available on www.nuget.org, with a fix for your issue.

Would you be able to validate that the formatter in the pre-release works as expected? You will have to make some changes to your configuration of the UDP sink, please see the migration instructions here.

@kevinbrechbuehl
Copy link
Author

This works as expected, thank you :)

@FantasticFiasco
Copy link
Owner

Perfect!

I will have to await confirmation from another user before I release a new official version, but I hope it shouldn't take that long, unless he has vacation, but then I'll release the new version anyway.

Thanks for the support! Keep up the good work!

FantasticFiasco added a commit that referenced this issue Jul 8, 2019
Closes #42
Closes #45
Closes #51
@FantasticFiasco
Copy link
Owner

Release v6.0.0 is now live on nuget.org.

Thank you for reporting the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants