huorswords / Microsoft.Extensions.Logging.Log4Net.AspNetCore

Allows to configure Log4net as Microsoft Extensions Logging handler on any ASP.NET Core application. Original code proposal by @anuraj --> https://dotnetthoughts.net/how-to-use-log4net-with-aspnetcore-for-logging/
Apache License 2.0
249 stars 123 forks source link

Log4net in asp.net core gives wrong logging output for class' method name #97

Closed zinbac000 closed 3 years ago

zinbac000 commented 4 years ago

I'm using this extension to add log4net to my project. I created the config file and give the config value for the conversionPattern like this: %date %5level %logger.%method - MESSAGE: %message%newline %exception But the output in the log file turned out to be like this: 2020-10-22 17:45:40,271 INFO Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.LogMessage [0] - MESSAGE: Successfully validated the token. 2020-10-22 17:45:40,314 INFO Microsoft.AspNetCore.Authorization.DefaultAuthorizationService.LogMessage [0] - MESSAGE: Authorization was successful. The problem is that the method part of the logging output is always LogMessage. Not the method name that i wanted it to be.

Jotune commented 4 years ago

Related to #80

The issue looks to happen when using a wrapper class arround Log4net. Source.

huorswords commented 4 years ago

Hi @zinbac000 ,

Sorry to hear about your problems, but yes, I understood your point, and maybe I'm wrong but it is not possible to get your method name without using Reflection, and this usage will impact in your perfromance when using this library.

Currently, log4net is extracting the %method parameter from the point where the lo4net Log's methods are invoked, and as this library is encapsulating it in a ILogger implementation, the name of the method will be always LogMessage.

As a workaround, I suggest to do it using scope parameters. Please, take a look on this section of the documentation.

lscorcia commented 3 years ago

Hello @huorswords , thanks for creating this component!

There is actually a quite simple way to restore the method name functionality without additional impacts on performance, please refer to https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore/pull/103 . It can be further improved, but that's the essential fix.