开发环境:
VS2013,
Asp.Net MVC 4.0,
.Net Framework 4.0,
Log4net 1.2.13.0,
Mysql.Data.dll,6.8.3.0
设置步骤:
1.在站点根目录下,创建一个log4net.config的XML文件。(文件名称可以任意,我们这里叫做log4net.config)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="AdoNetAppender" />
<!--<appender-ref ref="RollingFile" />-->
<!--<appender-ref ref="RollingLogFileAppender" />-->
</root>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<!-- 日志文件部分log输出格式的设定 -->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\Log_" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd'.txt'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<header value="------------------------------------------------------------" />
<ConversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="example.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data,Version=6.8.3.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" />
<connectionString value="server=127.0.0.1;database=log4net_db;Uid=root;Pwd=123;" />
<commandText value="INSERT INTO log4net(`Date`,`Thread`,`Level`,`Logger`,`Message`,`Exception`) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
</log4net>
</configuration>
2.项目可视化下,Properties/AssemblyInfo.cs中添加:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
3.global.asax.cs文件中执行:
protected void Application_Start()
{
//独立的log4net.config
log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
4.调用写日志方法:
ILog logger = LogManager.GetLogger(typeof(AlbumServcie));
logger.ErrorFormat("GetAlbumModel IsIISCache={0} throw Exception={1}", IsIISCache, ex.Message);
注意:
在log4net。config中,删除:
<configSections>
<section name=
"log4net"
type=
"log4net.Config.Log4NetConfigurationSectionHandler, log4net"
/>
</configSections>
<configSections>
<section name=
"log4net"
type=
"log4net.Config.Log4NetConfigurationSectionHandler, log4net"
/>
</configSections>
参考:
1.http://www.cnblogs.com/mahope/archive/2006/04/14/375221.html
2.http://logging.apache.org/log4net/release/config-examples.html
3.http://blog.csdn.net/zhoufoxcn/article/details/2220533