Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can log4net output Json?

Tags:

json

c#

wpf

log4net

I've seen a couple extensions for log4net that claim to create json to the log file, but the format is never valid json, meaning the collection is not in an array and not coma separated. Am I using it wrong or is there just no way to use log4net with json?

<appender name="SessionFileAppender" type="log4net.Appender.FileAppender">
  <file value="Session.log" />
  <appendToFile value="false" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type='log4net.Layout.SerializedLayout, log4net.Ext.Json'>
    <decorator type='log4net.Layout.Decorators.StandardTypesDecorator, log4net.Ext.Json' />
    <default />
    <remove value='message' />
    <member value='message:messageobject' />
  </layout>
</appender>

Output is:

{"date":"2017-01-29T13:45:50.7345813-05:00","level":"DEBUG","appname":"MyApp.vshost.exe","logger":"MainWindow","thread":"9","ndc":"(null)","message":"Loading new UI instance"}
{"date":"2017-01-29T13:45:50.7380842-05:00","level":"DEBUG","appname":"MyApp.vshost.exe","logger":"MainWindow","thread":"9","ndc":"(null)","message":"Loading internal localization file"}
{"date":"2017-01-29T13:45:50.7510970-05:00","level":"DEBUG","appname":"MyApp.vshost.exe","logger":"MainWindow","thread":"9","ndc":"(null)","message":"Initializing UI"}

which is close, but not really valid json.

like image 565
Wobbles Avatar asked Jan 29 '17 18:01

Wobbles


People also ask

What is the use of log4net?

log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.

What are log4net Appenders?

For log4net to know where to store your log messages, you add one or more appenders to your configuration. An appender is a C# class that can transform a log message, including its properties, and persist it somewhere. Examples of appenders are the console, a file, a database, an API call, elmah.io, etc.

What is log4net in API?

Log4net is an open source project based on the work of many authors. It allows the developer to control which log statements are output with arbitrary granularity. It is fully configurable at runtime using external configuration files. Almost every large application includes its own logging or tracing API.

Where does log4net write to?

In your case, the log file will be in bin\Debug\netcoreapp3.


1 Answers

download log4net.Ext.Json nuget package

add following config settings to app.config/web.config files

<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <file value="logs\log-file-name.json" />
  <rollingStyle value="Date" />
  <datePattern value="yyyy-MM-dd" />
  <PreserveLogFileNameExtension value="true" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <maxSizeRollBackups value="10" />
  <dateTimeStrategy 
  type="log4net.Appender.RollingFileAppender+UniversalDateTime" />

  <!--text formatted log4net logging-->
  <!--<layout type="log4net.Layout.PatternLayout">
    --><!--check conversion patterns from 
   https://logging.apache.org/log4net/--><!--
    --><!--<conversionPattern value="%utcdate{ABSOLUTE} UTC %c{1} - %m%n" 
  />--><!--
    <conversionPattern value="%date [%thread] %-5level %logger - 
  %message%newline" />
  </layout>-->

  <!--json formatted log4net logging-->
  <layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json">
    <decorator type="log4net.Layout.Decorators.StandardTypesDecorator, 
  log4net.Ext.Json" />
    <member value="date:date" />
    <member value="level:level" />
    <member value="logger:logger" />
    <member value="message:messageObject" />
    <member value="exception:exception" />
  </layout>
</appender>
<root>
  <!--Options are "ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" and 
"OFF".-->
  <level value="DEBUG" />
  <appender-ref ref="RollingFile" />
</root>
</log4net>

here is a sample call with ninject

Kernel = new StandardKernel(); 
Kernel.Bind<ILog>().ToMethod(c => 
LogManager.GetLogger(typeof(YourClassName))).InSingletonScope();
var log = Kernel.Get<ILog>();
log.debug("testing log4net json");
like image 131
oetzi Avatar answered Oct 11 '22 08:10

oetzi