The correct way to use log4net (log naming)

There are two ways to configure and use log4net. Firstly, when can I configure my own appender and its associated log:

<!-- 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 a journal, I can do the following:

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

Another way to use this is to configure root the way I want:

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

And in this case, I can record the following messages:

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

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

How do you use log4net? Each part of the system writes to its own log, or everything is written to the registrar by default, and the configuration decides what to do next?

+69
c # logging log4net episerver
Aug 17 2018-11-11T00:
source share
4 answers

Regarding how you register messages inside the code, I would choose the second approach:

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

If the messages sent to the log above will be "named" using a fully qualified Bar type, for example.

 MyNamespace.Foo.Bar [INFO] message 

The advantage of this approach is that it is the de facto standard for organizing logging, and also allows you to filter your log messages by namespace. For example, you can indicate that you want to log an INFO level message, but increase the level of logging for Bar specifically for 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 log by name is a powerful log4net feature, if you just log all your messages to "myLog" , you lose most of this power!

As for the EPiServer CMS, you can use the above approach to specify a different logging level for the CMS and your own code.

For further reading, here is a code article that I wrote in a journal:

+83
Aug 17 2018-11-11T00:
source share

My answer may be delayed, but I think it can help newbies. You should not record if changes are not made, as shown below.

2 When implementing Log4net, files must be modified.




  • Add the link log2net.dll to the project.
  • app.config
  • The class file in which the logs will run.

Inside [app.config]:

First, in the "configSections" section, you need to add the code snippet below;

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

Then, in the "configuration" block, you should write a code snippet below. (This piece of code is customized to fit my needs, but it works like a 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> 

Inner call class:

Inside the class you are going to use this log4net in, you need to declare a code snippet below.

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

You are now ready for the call log, wherever you want, in the same class. The following is one of the methods that you can call when performing operations.

 log.Error("message"); 
+5
Dec 31 '16 at 7:01
source share

Instead of calling my calling class, I started using the following:

 private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 

That way, I can use the same line of code in every class that log4net uses, without forgetting to change the code when copying and pasting. Alternatively, I could create a logging class and inherit each class from my logging class.

+2
Mar 28 '17 at 16:02
source share

The disadvantage of the second approach is the large repository with created registrars. These registrars do the same if root is defined and class classifiers are not defined. In the standard scenario for a production system, several registrars are used for a group of classes. Sorry for my English.

0
May 09 '17 at 8:23 a.m.
source share



All Articles