Method name in log4net c #

I created a C # wrapper for log4net.

It has methods Debug () and Error ().

I want to write the name of the method that records the record, but if I try to use the% method conversion pattern, it just prints Debug, which is the name of the shell method.

Is there a way to print the full method stack?

eg.

Instead of debugging -> SomeLoggingActionInSomeClass.Debug?

Wrapper class code:

public static class Logger { private static ILog _log; static Logger() { log4net.Config.XmlConfigurator.Configure(); _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); } public static void Init() { } public static void Debug(string message) { _log.Debug(message); } 

Call Class Code:

 W6CustomizationLogger.Logger.Debug("Backup(): START"); 
+4
source share
2 answers

Discard this shell.

Configuration

To initialize logging, you can easily configure it in your launch project.

  • Click on the arrow next to your β€œProperties” in your startup project in Solution explorer
  • Double click on assembly information

enter image description here

3 Add the following:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

Screenshot:

enter image description here

Using

Now in your classes just add:

 public class YourClass { private ILog _logger = LogManager.GetLogger(typeof(YourClass)); // [....] } 

And in your log4net.config you can now use the logger property in the output:

 <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %-7level %-40logger %message%newline" /> </layout> 

What will the namespace and type name print on each line of the log ( -7 and -40 fill the names so that I get straight columns).

Another great thing is that you can also use a filter in the namespace (so that all database classes are registered in "databases.log", etc.).

 <appender name="DatabaseAppender" type="log4net.Appender.RollingFileAppender"> <file value="C:\Logs\MyApp\Database.log" /> <rollingStyle value="Composite" /> <datePattern value=".yyyy-MM-dd'.log'" /> <appendToFile value="true" /> <maximumFileSize value="50MB" /> <maxSizeRollBackups value="5" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date % level@ %thread [%logger] %message%newline" /> </layout> <!-- Namespace/Type filter --> <filter type="log4net.Filter.LoggerMatchFilter"> <loggerToMatch value="CompanyName.DatabaseNamespace"/> </filter> <filter type="log4net.Filter.DenyAllFilter" /> </appender> 

You can also use %type{1} if %logger get only the class name.

+6
source

If you really have to use a wrapper class, the best way is to get the calling method in the wrapper, and then save this as a property:

 var stackFrames = new StackTrace().GetFrames(); var callingframe = stackFrames.ElementAt(1); var method = callingframe .GetMethod().Name; // Store in log4net ThreadContext: ThreadContext.Properties["method"] = method; 

Then, in the layout job, the property:

 <conversionPattern value = ""Method: %property{method}"" /> 

There is also a way to only allow this using a layout, you would use %stacktrace in your layout to get the call stack, in particular %stacktrace{2} , to get the calling method.

Note that when you use this, the entire stack is logged, including the wrapper method.

Output Example:

log4net.Tests.Stacktrace_Tests.StackTrace_In_PatternLayout> log4net.Tests.Wrapper.Debug

+2
source

All Articles