在框架中配置日志分2步,一个是在帮助项目Zy.Utilities--Zy.Utility.Core中新建log类,封装写入日志方法,还需要在Zy.Utility.Core添加 log4net 的引用
1、log实现类
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Zy.Utility.Core
{
/// <summary>
/// 日志处理类
/// </summary>
public static class Log
{
public static void Error(string msg)
{
log4net.ILog log = log4net.LogManager.GetLogger(msg);
log.Error(msg);
} public static void Info(string msg)
{
log4net.ILog log = log4net.LogManager.GetLogger(msg);
log.Info(msg);
} public static void Warning(string msg)
{
log4net.ILog log = log4net.LogManager.GetLogger(msg);
log.Warn(msg);
} public static void Error(string msg, Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(msg);
log.Error("Error", ex);
} public static void Info(string msg, Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(msg);
log.Info("Info", ex);
} public static void Warning(string msg, Exception ex)
{
log4net.ILog log = log4net.LogManager.GetLogger(msg);
log.Warn("Warning", ex);
}
}
}
2、在Web项目的web.config中配置log4,配置完后,启动项目后会在ZyCommon-ZyWeb项目中生成一个Log的文件夹
<log4net>
<!--数据日志-->
<appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="Log\Info\" />
<param name="AppendToFile" value="true" />
<param name="rollingStyle" value="Date" />
<param name="datePattern" value="yyyy-MM-dd.'Info.log'" />
<param name="staticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%n%n 记录时间:%date %n日志级别: %-5level %n出错类:%logger %n错误描述:%message %newline%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<!--错误日志-->
<appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="Log\Error\" />
<param name="AppendToFile" value="true" />
<param name="rollingStyle" value="Date" />
<param name="datePattern" value="yyyy-MM-dd.'error.log'" />
<param name="staticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%n%n 记录时间:%date %n日志级别: %-5level %n出错类:%logger %n错误描述:%message %newline%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<!--调试日志-->
<appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="File" value="Log\Debug\" />
<param name="AppendToFile" value="true" />
<param name="rollingStyle" value="Date" />
<param name="datePattern" value="yyyy-MM-dd.'debug.log'" />
<param name="staticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%n%n 记录时间:%date %n日志级别: %-5level %n出错类:%logger %n错误描述:%message %newline%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<root>
<level value="DEBUG" />
<!--文件形式记录日志-->
<appender-ref ref="ErrorRollingFileAppender" />
<appender-ref ref="DebugRollingFileAppender" />
<appender-ref ref="InfoRollingFileAppender" />
</root>
</log4net>
Log文件夹下分为Debug、Error、Info文件夹,分别写入错误级别不同的日志信息
开发中报错后,错误信息会写入Error文件夹中,按配置规则每天生成一个txt文件夹,调试时也可以写入日志中