一、引用log4net.dll网上找一个即可
二、新建一个log4net的配置文件,log4net.cfg.xml,属性:内容,总是拷贝 很重要否则编译后不会创建Log File
<?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name="rollingFile_App" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logs\CPBMP_WinForm" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value=".yyyy.MM.dd.'log'" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" /> </layout> </appender> <!--<root> --><!--如果只需要看看Sql设置INFO就够了,如果你要调试可以设置为DEBUG或ALL--><!-- <priority value="INFO" /> <appender-ref ref="rollingFile_SQL" /> </root>--> <logger name="CPBMP_WinForm"> <level value="Debug" /> <appender-ref ref="rollingFile_App" /> </logger> </log4net>
三、新建一类
View Code
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
namespace CPBMP_WinForm
{
public sealed class NewLogManager
{
//Pre-defined logger name in web.config or app.config
public const string LoggerName = "CPBMP_WinForm";
public const int EmptyFunctionKey = -1;
public const int AnonymouseUserKey = -1;
public static readonly ILog Logger = log4net.LogManager.GetLogger(LoggerName);
/// <summary>
/// Add a error log
/// </summary>
/// <param name="errorMessage"></param>
public static void AddError(string errorMessage)
{
Logger.Error(errorMessage);
}
/// <summary>
/// Add a error log with an exception
/// </summary>
/// <param name="errorMessage"></param>
/// <param name="ex"></param>
public static void AddError(string errorMessage, Exception ex)
{
Logger.Error(errorMessage + System.Environment.NewLine + ex.Message + System.Environment.NewLine + ex.ToString());
}
/// <summary>
/// Add a warning log
/// </summary>
/// <param name="warnMessage"></param>
public static void AddWarn(string warnMessage)
{
Logger.Warn(warnMessage);
}
/// <summary>
/// Add a warning log with an exception
/// </summary>
/// <param name="warnMessage"></param>
/// <param name="ex"></param>
public static void AddWarn(string warnMessage, Exception ex)
{
Logger.Warn(warnMessage + System.Environment.NewLine + ex.Message + System.Environment.NewLine + ex.ToString());
}
/// <summary>
/// Add a info log
/// </summary>
/// <param name="infoMessage"></param>
public static void AddInfo(string infoMessage)
{
Logger.Info(infoMessage);
}
/// <summary>
/// Add a info log with an exception
/// </summary>
/// <param name="infoMessage"></param>
/// <param name="ex"></param>
public static void AddInfo(string infoMessage, Exception ex)
{
Logger.Info(infoMessage + System.Environment.NewLine + ex.Message + System.Environment.NewLine + ex.ToString());
}
/// <summary>
/// Add a debug log
/// </summary>
/// <param name="debugMessage"></param>
public static void AddDebug(string debugMessage)
{
Logger.Debug(debugMessage);
}
/// <summary>
/// Add a debug log with an exception
/// </summary>
/// <param name="debugMessage"></param>
/// <param name="ex"></param>
public static void AddDebug(string debugMessage, Exception ex)
{
Logger.Debug(debugMessage + System.Environment.NewLine + ex.Message + System.Environment.NewLine + ex.ToString());
}
/// <summary>
/// Add a database error log
/// </summary>
/// <param name="objectName">table or view name</param>
/// <param name="dbAction">either retrieve, update, delete</param>
/// <param name="ex"></param>
public static void AddDatabaseError(string objectName, DatabaseAction dbAction, Exception ex)
{
string logMessage = string.Empty;
switch ( dbAction )
{
case DatabaseAction.Select:
logMessage = "Error occurred when retrieve data from table " + objectName;
break;
case DatabaseAction.Insert:
logMessage = "Error occurred when insert data to table " + objectName;
break;
case DatabaseAction.Update:
logMessage = "Error occurred when update data to table " + objectName;
break;
case DatabaseAction.Delete:
logMessage = "Error occurred when delete data from table " + objectName;
break;
}
AddError(logMessage, ex);
}
}
/// <summary>
/// Enumeration for database action type
/// </summary>
public enum DatabaseAction
{
Select,
Insert,
Update,
Delete
}
}
四、重要一步,启动时加载配置文件,并写Log
FileInfo file = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.cfg.xml"));
log4net.Config.XmlConfigurator.Configure(file);
NewLogManager.AddInfo("Application is starting...");