log4net插入access自定义字段

时间:2023-03-09 19:41:56
log4net插入access自定义字段

1.创建表格

2.创建log4net.xml,并设置属性始终复制,关键属性

 <bufferSize value="1" />
 <conversionPattern value="%property{usernm}" />

|DataDirectory| 如果是winform,在项目下加个文件夹App_Data然后将access数据库放进去。
在程序运行开始加上这个方法:
 string p = AppDomain.CurrentDomain.BaseDirectory;
if (p.IndexOf("\\bin\\") > )
{
if (p.EndsWith("\\bin\\Debug\\"))
p = p.Replace("\\bin\\Debug", "");
if (p.EndsWith("\\bin\\Release\\"))
p = p.Replace("\\bin\\Release", "");
}
if (!p.EndsWith("App_Data\\"))
p = p + "App_Data\\";
AppDomain.CurrentDomain.SetData("DataDirectory", p);
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<!--站点日志配置部分-->
<log4net>
<root>
<!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
<!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录-->
<!--如果没有定义LEVEL的值,则缺省为DEBUG-->
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionString value=" Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\site.accdb;Persist Security Info=False;" />
<commandText value="INSERT INTO tbLog ([Date],[Thread],[Level],[Logger],[Message],[usernm]) VALUES (@log_date, @thread, @log_level, @logger, @message,@usernm)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date" />
</layout>
</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="1024" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@usernm" />
<dbType value="String" />
<size value="1024" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{usernm}" />
</layout>
</parameter>
</appender>
<logger name="FCore" additivity="true">
<appender-ref ref="AdoNetAppender_Access" />
<level value="ALL" />
</logger>
</log4net>
</configuration>

3.创建log4net帮助

  class LogHelper
{
/// <summary>
/// lock对象
/// </summary>
private static object lockLog = new object(); static string _logpath;
private static string LogPath
{
get
{
if (_logpath == null)
_logpath = AppDomain.CurrentDomain.BaseDirectory + "\\Log4net.xml";
return _logpath;
}
}
private static ILog _log;
/// <summary>
/// 记录Log信息
/// </summary>
public static ILog ToLog
{
get
{
if (_log == null)
{
lock (lockLog)
{
System.IO.FileInfo file = new System.IO.FileInfo(LogPath);
log4net.Config.XmlConfigurator.ConfigureAndWatch(file);
//log4net.Config.XmlConfigurator.Configure(file);
_log = log4net.LogManager.GetLogger("FCore");
GlobalContext.Properties["usernm"] = "用户名";
}
}
return _log;
}
}
} /// <summary>
/// Log对外执行文件
/// </summary>
public class Log
{
/// <summary>
/// 记录Error日志
/// </summary>
/// <param name="msg"></param>
public static void Error(string msg)
{
LogHelper.ToLog.Error(msg);
}
/// <summary>
/// 记录Warn日志
/// </summary>
/// <param name="msg"></param>
public static void Warn(string msg)
{
LogHelper.ToLog.Warn(msg);
}
/// <summary>
/// 记录Info日志
/// </summary>
/// <param name="msg"></param>
public static void Info(string msg)
{
LogHelper.ToLog.Info(msg);
}
/// <summary>
/// 记录Debug日志
/// </summary>
/// <param name="msg"></param>
public static void Debug(string msg)
{
LogHelper.ToLog.Debug(msg);
}
}

4.调用

 Log.Info(MethodBase.GetCurrentMethod().DeclaringType.ToString() + ":" + GetMethodName());

  public static string GetMethodName()
{
var method = new StackFrame().GetMethod(); // 这里忽略1层堆栈,也就忽略了当前方法GetMethodName,这样拿到的就正好是外部调用GetMethodName的方法信息
var property = (
from p in method.DeclaringType.GetProperties(
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.NonPublic)
where p.GetGetMethod(true) == method || p.GetSetMethod(true) == method
select p).FirstOrDefault();
return property == null ? method.Name : property.Name;
}

另一种写入本地

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<!--站点日志配置部分-->
<log4net>
<root>
<!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
<!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录-->
<!--如果没有定义LEVEL的值,则缺省为DEBUG-->
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<appender name="DateAppender" type="log4net.Appender.RollingFileAppender">
<bufferSize value="100" />
<file value="C:\log\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<datePattern value="yyyyMMdd&quot;.log&quot;" />
<!--<param name="StaticLogFileName" value="false"/>-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="时间:%date/线程:[%thread]/level:%-5level/logger:%logger/property:[%property{NDC}]/method:%method/毫秒:%r/信息:%message%newline" />
</layout>
</appender>
<logger name="FCore" additivity="true">
<appender-ref ref="DateAppender" />
<level value="INFO" />
</logger>
</log4net>
</configuration>