Создайте свой класс-оболочку следующим образом ...
public static class LogFourNet
{
// Define a static logger variable so that it references the
private static readonly ILog Log = LogManager.GetLogger(typeof(LogFourNet));
static LogFourNet()
{
XmlConfigurator.Configure(
new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
Log.Info("Log4net is configured.");
}
public static void Info(object currentObj, string msg, [CallerLineNumber] int lineNumber = 0,
[CallerFilePath] string caller = "", [CallerMemberName] string memberName = "")
{
Log.Info("[" + currentObj.GetType().Namespace + "." +
Path.GetFileNameWithoutExtension(caller) + "." + memberName + ":" + lineNumber + "] - " + msg);
}
public static void Debug(object currentObj, string msg, [CallerLineNumber] int lineNumber = 0,
[CallerFilePath] string caller = "", [CallerMemberName] string memberName = "")
{
Log.Debug("[" + currentObj.GetType().Namespace + "." +
Path.GetFileNameWithoutExtension(caller) + "." + memberName + ":" + lineNumber + "] - " + msg);
}
public static void Error(object currentObj, string msg, [CallerLineNumber] int lineNumber = 0,
[CallerFilePath] string caller = "", [CallerMemberName] string memberName = "")
{
Log.Error("[" + currentObj.GetType().Namespace + "." +
Path.GetFileNameWithoutExtension(caller) + "." + memberName + ":" + lineNumber + "] - " + msg);
}
}
Настройте файл log4net.config следующим образом ...
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="logfile.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p - %m%n" />
</layout>
</appender>
<root>
<!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<level value="ALL" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
Теперь просто используйте над этими методами вот так ...
// need to pass this(current obj) as we want to log full class name
LogFourNet.Debug(this, "started something from wrapper");
output:
-------
2017-02-04 15:38:37,549 [1] DEBUG [WebAPI_DI.Startup.Configuration:25] - started something from wrapper