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.
Polaris878
source share