Different applications for each method

Just started using log4net and tried to figure out the hierarchy of configurations and logs. Is this hierarchy based on namespaces or a hierarchy of classes and methods / functions?

Suppose I have the following class structure ...

public class MyClass { private static readonly ILog log = LogManager.GetLogger(typeof(MyClass)); public void Method1() { log4net.info("message"); } public void Method2() { log4net.info("message"); } } 

Is it possible to set in config for log4net.info in method1 use one appender and log4net.info in method 2 to use another application, even if they do not match the type, for example. SmtpAppender If so, what will the config look like. here is my first attempt.

 <appender name="SMTP1" type="log4net.Appender.SMTPAppender"> </appender> <appender name="SMTP2" type="log4net.Appender.SMTPAppender"> </appender> <logger name="MyClass.Method1"> <level value="INFO" /> <appender-ref ref="SMTP1" /> </logger> <logger name="MyClass.Method2"> <level value="INFO" /> <appender-ref ref="SMTP2" /> </logger> 
+7
source share
2 answers

The hierarchy is based on "names." What does it mean? Well, you can specify a namespace in your XML log file (e.g. Foo.Bar ) and then extract the logger for the class in that namespace using the GetLogger method, which accepts Type . Any "under" namespace in Foo.Bar inherits Foo.Bar config logger.
In addition, you can get the logger based on any old string using the GetLogger method, which takes a string .

You can select registrars in several ways. Most noticeably, on Type or on string .

Being able to get a string , you can really call your registrars something and get them using something. What you have now will not work, because log4net will retrieve the log based on the class ... so you will use the same log for both methods.

Why do you want to create two registrars:

 public class MyClass { private static readonly ILog log = LogManager.GetLogger(typeof(MyClass).Name + "." + "Method1"); private static readonly ILog log2 = LogManager.GetLogger(typeof(MyClass).Name + "." + "Method2"); public void Method1() { log.info("message"); } public void Method2() { log2.info("message"); } } 

Here is the same xml log file:

 <logger name="MyClass.Method1"> <level value="INFO" /> <appender-ref ref="SMTP1" /> </logger> <logger name="MyClass.Method2"> <level value="INFO" /> <appender-ref ref="SMTP2" /> </logger> 

I'm not too good at .Net, so maybe someone can find a better / more reliable way to get the loggers for each method using some ninja Reflection, but this is the best I could do.

+6
source

The same log cannot route multiple applications based on the current method, at least not directly. However, what you can do is use log4net filters

do what you want. The log4net structure populates each LoggingEvent with the location information you need (for example, the method name) to perform the necessary filtering. You will probably need to write a custom IFilter to filter it. IFilter is a pretty trivial interface to implement: 1 method / 1property. Your Decide() filter method makes 1 of 3 decisions for each registration event passed to it:

  • Deny. Discards the registration event. The event is not logged, and there are no subsequent filters in the filter chain for the application.
  • Usual. The registration event is passed to the next filter in the filter chain for this application.
  • To accept. The event is logged. No subsequent filters in the filter chain are posted.

Then add a filter to the filter chain for each application you add. This can be done on the fly, or it can be configured through your log4net.config.

However, you should be aware that the quality of the location information you receive depends on whether it is a Debug or Release assembly ... and, I believe, it has debugging symbols available.

You can also look at log4net for various contexts (and contextual stacks)

if you need to add some contextual information on the basis of which your filtering will be created.

+6
source

All Articles