参考学习网址:
http://blog.csdn.net/weifangyh/article/details/8524380
http://blog.csdn.net/dragon_ton/article/details/71601339
https://www.cnblogs.com/kissazi2/p/3392094.html
https://www.cnblogs.com/zeroone/p/3606160.html
1.在AssemblyInfo中添加
[assembly: log4net.Config.XmlConfigurator()]
2.新建一个控制台应用程序,在项目中引用log4net.dll,然后修改app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
</configSections>
<log4net>
<!--定义输出到文件中-->
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<!--定义文件存放位置-->
<file value="Log\\LogInfo.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明-->
<footer value="" />
<!--输出格式-->
<!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info-->
<conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level - 描述:%message%newline" />
</layout>
</appender>
<appender name="LogFileAppender_Size" type="log4net.Appender.RollingFileAppender">
<file value="Log\\LogInfo.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level - 描述:%message%newline" />
</layout>
</appender>
<appender name="LogFileAppender_Date" type="log4net.Appender.RollingFileAppender">
<file value="Log\\LogInfo.txt" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level - 描述:%message%newline" />
</layout>
</appender>
<root>
<!--日志记录方式-->
<appender-ref ref="LogFileAppender_Date" />
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
2.新建一个loggerhelper类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using System.Reflection;
namespace TestLog4Net
{
public static class LoggerHelper
{
private static ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// 获取当前 Ilog 对象
/// </summary>
public static ILog GetILog
{
get
{
return log;
}
}
/// <summary>
/// 记录错误日志
/// </summary>
/// <param name="message"></param>
/// <param name="ex"></param>
public static void Error(object message, Exception ex)
{
log.Error(message, ex);
}
/// <summary>
/// 记录严重错误
/// </summary>
/// <param name="message"></param>
/// <param name="ex"></param>
public static void Fatal(object message, Exception ex)
{
log.Fatal(message, ex);
}
/// <summary>
/// 记录一般信息
/// </summary>
/// <param name="message"></param>
public static void Info(object message)
{
log.Info(message);
}
/// <summary>
/// 记录调试信息
/// </summary>
/// <param name="message"></param>
public static void Debug(object message)
{
log.Debug(message);
}
/// <summary>
/// 记录警告信息
/// </summary>
/// <param name="message"></param>
public static void Warn(object message)
{
log.Warn(message);
}
}
}
3.使用loggerhelper类
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestLog4Net
{
class Program
{
static void Main(string[] args)
{
LoggerHelper.Error("这里存在错误",null);
Console.ReadKey();
}
}
}