Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way of using log4net (logger naming)

There are two ways of configuring and using log4net. First one is when I can configure my own appender and associated logger:

<!-- language: xml -->  <appender name="myLogAppender" type="log4net.Appender.RollingFileAppender" >     <file value="Logs\myLog.log" />     <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%date %level - %message%n" />     </layout> </appender>  <logger name="myLog">     <level value="All"></level>     <appender-ref ref="myLogAppender" /> </logger> 

And then when I want to write something in log, I can do the following:

ILog log = LogManager.GetLogger("myLog"); log.Info("message"); 

Another way to use it is to configure root to be as detailed as I want:

<!-- language: xml -->  <root>     <level value="Error" />     <appender-ref ref="myLogAppender" /> </root> 

And in this case I can log messages like this:

ILog log = LogManager.GetLogger(typeof(Bar)); log.Info("message"); 

The benefits of second approach is that you can enable or disable some messages on the fly. But the problem is that I'm developing in EPiServer CMS and it has its own logging system that uses log4net and if I enable info logging at root level, then a lot of system logs will be written.

How do you use log4net? Each part of a system writes in its own logger, or everything is written in default logger, and configuration decides what to do next?

like image 803
Sly Avatar asked Aug 17 '11 07:08

Sly


People also ask

Is log4net structured logging?

log4net doesn't support the concept of structured logging. Like shown in the conversionPattern element in the XML configuration, you have some variables to play with when writing to the storage. But including properties like FirstName in the Serilog example isn't available.

Where do log4net logs go?

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

How do I initialize log4net?

Configuration InitializationConfigureAndWatch to initialize log4net setup as per the config setup in your main entry method like main method of program class. Method “ ConfigureAndWatch ” configures log4net using the file specified, monitors the file for changes and reloads the configuration if a change is detected.


2 Answers

Regarding how you log messages within code, I would opt for the second approach:

ILog log = LogManager.GetLogger(typeof(Bar)); log.Info("message"); 

Where messages sent to the log above will be 'named' using the fully-qualifed type Bar, e.g.

MyNamespace.Foo.Bar [INFO] message 

The advantage of this approach is that it is the de-facto standard for organising logging, it also allows you to filter your log messages by namespace. For example, you can specify that you want to log INFO level message, but raise the logging level for Bar specifically to DEBUG:

<log4net>     <!-- appenders go here -->     <root>         <level value="INFO" />         <appender-ref ref="myLogAppender" />     </root>      <logger name="MyNamespace.Foo.Bar">         <level value="DEBUG" />     </logger> </log4net> 

The ability to filter your logging via name is a powerful feature of log4net, if you simply log all your messages to "myLog", you loose much of this power!

Regarding the EPiServer CMS, you should be able to use the above approach to specify a different logging level for the CMS and your own code.

For further reading, here is a codeproject article I wrote on logging:

  • The Art Of Logging
like image 58
ColinE Avatar answered Sep 26 '22 03:09

ColinE


My Answer might be coming late, but I think it can help newbie. You shall not see logs executed unless the changes are made as below.

2 Files have to be changes when you implement Log4net.


  1. Add Reference of log4net.dll in the project.
  2. app.config
  3. Class file where you will implement Logs.

Inside [app.config] :

First, under 'configSections', you need to add below piece of code;

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 

Then, under 'configuration' block, you need to write below piece of code.(This piece of code is customised as per my need , but it works like charm.)

<log4net debug="true">     <logger name="log">       <level value="All"></level>       <appender-ref ref="RollingLogFileAppender" />     </logger>      <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">       <file value="log.txt" />       <appendToFile value="true" />       <rollingStyle value="Composite" />       <maxSizeRollBackups value="1" />       <maximumFileSize value="1MB" />       <staticLogFileName value="true" />        <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%date %C.%M [%line] %-5level - %message %newline %exception %newline" />       </layout>     </appender> </log4net> 

Inside Calling Class :

Inside the class where you are going to use this log4net, you need to declare below piece of code.

 ILog log = LogManager.GetLogger("log"); 

Now, you are ready call log wherever you want in that same class. Below is one of the method you can call while doing operations.

log.Error("message"); 
like image 20
Ankur Soni Avatar answered Sep 24 '22 03:09

Ankur Soni