Logger参考
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net " />
</configSections>
<log4net>
<appender name="ApiRollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="./log/app" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Composite" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaximumFileSize" value="1GB" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value=""_"yyyyMMdd".log"" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%5p] - %m%n" />
</layout>
<Encoding value="UTF-8" />
</appender>
<appender name="VsTraceAppender" type="xxxx.app.common.util.VsTraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="ApiRollingLogFileAppender" />
<appender-ref ref="VsTraceAppender" />
</root>
</log4net>
:
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using log4net;
using log4net.Core;
using log4net.Appender;
namespace mvapi.app.common.util
{
<summary>
</summary>
public static class logger
{
<summary>
</summary>
private static ILog mylogger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
<summary>
</summary>
private const string MSG_FORMAT = "{0},{1},{2},{3}";
<summary>
</summary>
public static void Fatal(string msg, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string callerMemberName = "", [CallerLineNumber] int callerLineNumber = 0)
{
mylogger.Fatal(string.Format(MSG_FORMAT, callerFilePath, callerMemberName, callerLineNumber, msg));
}
<summary>
</summary>
public static void Error(string msg, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string callerMemberName = "", [CallerLineNumber] int callerLineNumber = 0)
{
mylogger.Error(string.Format(MSG_FORMAT, callerFilePath, callerMemberName, callerLineNumber, msg));
}
<summary>
</summary>
public static void Warn(string msg, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string callerMemberName = "", [CallerLineNumber] int callerLineNumber = 0)
{
mylogger.Warn(string.Format(MSG_FORMAT, callerFilePath, callerMemberName, callerLineNumber, msg));
}
<summary>
</summary>
public static void Info(string msg, [CallerFilePath] string callerFilePath = "",[CallerMemberName] string callerMemberName = "",[CallerLineNumber] int callerLineNumber = 0)
{
mylogger.Info(string.Format(MSG_FORMAT, callerFilePath, callerMemberName, callerLineNumber, msg));
}
<summary>
</summary>
public static void Debug(string msg, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string callerMemberName = "", [CallerLineNumber] int callerLineNumber = 0)
{
mylogger.Debug(string.Format(MSG_FORMAT, callerFilePath, callerMemberName, callerLineNumber, msg));
}
}
}