log4net 独立配置文件设置

2018-06-22 07:48:20来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

1. 新建一个配置文件log4net.config,配置方法同成web.config或app.config一致;

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>

<log4net debug="true">
<logger name="myLogger">
<level value="INFO"/>
<appender-ref ref="AdoNetAppender_SqlServer"/>
</logger>

<!--sql server数据库1-->
<appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
<!-- BufferSize 为缓冲区大小,只有日志记录超5 条才会一块写入到数据库 -->
<!-- 或写为<param name="BufferSize" value="10" /> -->
<bufferSize value="0"/>
<!-- 引用 -->
<!--2.0这是对应sql2008 如是2000或2005另外配置-->
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089"/>

<!-- 连接数据库字符串 -->
<connectionString value="Data Source=.;Initial Catalog=Log;User ID=sa;Password=123456;" />
<!-- 插入到表Log -->
<commandText value = "INSERT INTO Mylogger ([EVENTTYPE],[TIMESTAMP],[EVENTCATEGORY],[EVENT_ID],[COMPUTERNAME],[MAC_ADDRESS],[USERNAME],[SOURCETYPE],[SOURCE],[DESCRIPTION],[COLLECTDATE]) VALUES (@Event_Type,@log_date, @EventCategory, @Event_ID, @ComputerName,@Mac_Address,@UserName,@SourceType,@Source,@Description,@CollectDate) "/>

<!-- 日志类型,这里均为3 -->
<parameter>
<parameterName value = "@Event_Type"/>
<dbType value = "Int32"/>
<!--<dbType value = "String"/>
<size value = "50"/>-->
<!-- LogComponent 是类所在的命名空间,MyLayout 是自定义属性所在的类,这是我们自己要写的部分,将在下面介绍。 -->
<layout type = "SuperAuth.Infrastructure.MyLayout">
<!-- 当用到property 时,就表明这是用户自定义的字段属性啦,是log4net 中所没有提供的字段。 -->
<conversionPattern value = "%property{Event_Type} "/>
</layout>
</parameter>

<!-- 日志记录时间,RawTimeStampLayout 为默认的时间输出格式 -->
<parameter>
<parameterName value = "@log_date"/>
<dbType value = "DateTime"/>
<layout type = "log4net.Layout.RawTimeStampLayout"/>
<!-- /这里呢是获取log4net 中提供的日志时间 -->
</parameter>

<!-- 日志分类描述 -->
<parameter>
<parameterName value = "@EventCategory"/>
<dbType value = "String"/>
<size value = "50"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{EventCategory}"/>
</layout>
</parameter>

<!-- 日志分类号 -->
<parameter>
<parameterName value = "@Event_ID"/>
<dbType value = "Int32"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{Event_ID}"/>
</layout>
</parameter>

<!-- 计算机IP -->
<parameter>
<parameterName value = "@ComputerName"/>
<dbType value = "String"/>
<size value = "50"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{ComputerName}"/>
</layout>
</parameter>

<!-- 计算机Mac 信息 -->
<parameter>
<parameterName value = "@Mac_Address"/>
<dbType value = "String"/>
<size value = "50 "/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{Mac_Address}"/>
</layout>
</parameter>

<!-- 登陆系统用户名 -->
<parameter>
<parameterName value = "@UserName"/>
<dbType value = "String"/>
<size value = "50"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{UserName}"/>
</layout>
</parameter>

<!-- 事件来源类型,这里默认为Rier -->
<parameter>
<parameterName value = "@SourceType"/>
<dbType value = "String"/>
<size value = "20"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{SourceType}"/>
</layout>
</parameter>

<!-- 事件来源 -->
<parameter>
<parameterName value = "@Source "/>
<dbType value = "String"/>
<size value = "50"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{Source}"/>
</layout>
</parameter>

<!-- 事件描述 -->
<parameter>
<parameterName value = "@Description "/>
<dbType value = "String"/>
<size value = "4000"/>
<layout type = "SuperAuth.Infrastructure.MyLayout">
<conversionPattern value = "%property{Description}"/>
</layout>
</parameter>

<!-- 日志收集时间 -->
<parameter>
<parameterName value = "@CollectDate"/>
<dbType value = "DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
</appender>

</log4net>
<system.web>
<compilation debug="true" targetFramework="4.6.2" />
<httpRuntime targetFramework="4.6.2" />
</system.web>

</configuration>

2.建立相关的类文件如下:

public class LogHelper
{
private LogHelper()
{
SetConfig();
}

private static readonly log4net.ILog loginfo =LogManager.GetLogger("myLogger");

private static bool IsLoadConfig = false;
private static void SetConfig()
{
XmlConfigurator.Configure();

}

/// <summary>
/// 记录日志
/// </summary>
/// <param name="info">提示信息</param>
public static void WriteLog(string info)
{
if (!IsLoadConfig)
{
SetConfig();
IsLoadConfig = true;
}
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}

/// <summary>
/// 记录日志
/// </summary>
/// <param name="info">提示信息</param>
public static void WriteLog(object info)
{
if (!IsLoadConfig)
{
SetConfig();
IsLoadConfig = true;
}
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}

/// <summary>
/// 记录异常
/// </summary>
/// <param name="info">错误</param>
/// <param name="ex">Exception</param>
public static void WriteLog(string info, Exception ex)
{
if (!IsLoadConfig)
{
SetConfig();
IsLoadConfig = true;
}
if (loginfo.IsErrorEnabled)
{
loginfo.Error(info, ex);
}


}

3.在使用类的地方引入log4net.config如下

[assembly: XmlConfigurator(ConfigFile= @"Log4net.config", Watch = true)]

4.最重要的一步,也是很多人出错的地方,右键log4net.config点击属性,按如下设置即可。

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:获取枚举值中的特性[Display]中的name值

下一篇:6种方法实现asp.net返回上一页