有一个 asp.net,dot net 4 web 应用程序,使用 log4net 进行日志记录。我们记录到 Windows 事件日志和 Oracle 11g 表。除了 Application_Start 之外,我们可以在任何地方登录到 oracle 表。真正令人讨厌的事情?它默默地失败了。打开控制台日志记录,并重定向到一个文件,我们开始记录之前的最后一个条目是“Added appender named [AdoNetAppender] to logger [root]”。Repository.Configured(对于 log4net-default-repository)此时对于所有记录器都是 true。颠倒配置文件中附加程序出现的顺序无效。
配置文件的 log4net 部分:
<log4net debug="true"> <!-- log4net.Internal.Debug set to true in appSettings, System.Diagnostics.Trace is output to file.>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<param name="LogName" value="AppLog" />
<param name="ApplicationName" value="AppName" />
<!-- Only log warnings and errors. Levels are ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
<threshold value="INFO" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{MM/dd/yyyy HH:mm:ss} [%property{log4net:HostName}] %-5level WEBUI[%logger] [%u] %message%newline" />
</layout>
</appender>
<appender name="AdoNetAppender" type="Logging.CustomAdoNetAppender, Logging"> <!-- So it can find the custom appender-->
<ConnectionStringName value="Database"/>
<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<bufferSize value="1"/><!--This is the number of log entries, not the size in bytes... "1" sets to no buffering -->
<commandText value="audit_tracking.Add_log" />
<commandType value="StoredProcedure" />
<parameter>
<parameterName value="I_Thread" />
<dbType value="AnsiString" /> <!--AnsiString is VarChar. "String" will assume NVarChar -->
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread"></conversionPattern>
</layout>
</parameter>
<parameter>
<parameterName value="I_Log_Level" />
<dbType value="AnsiString" />
<size value="225" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level"/>
</layout>
</parameter>
<parameter>
<parameterName value="I_Logger" />
<dbType value="AnsiString" />
<size value="225" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger"/>
</layout>
</parameter>
<parameter>
<parameterName value="I_Message" />
<dbType value="AnsiString" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message"/>
</layout>
</parameter>
<parameter>
<parameterName value="I_Exception_Str" />
<dbType value="AnsiString" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%exception"/>
</layout>
</parameter>
<parameter>
<parameterName value="I_Server_Name" />
<dbType value="AnsiString" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{log4net:HostName}"/>
</layout>
</parameter>
<parameter>
<parameterName value="I_User_Name" />
<dbType value="AnsiString" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{UserName}"/>
</layout>
</parameter>
<parameter>
<parameterName value="I_User_Id" />
<dbType value="Int64" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{UserId}"/>
</layout>
</parameter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="EventLogAppender" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
C#部分:
protected void Application_Start(object sender, EventArgs e)
{
read_init_data();//reads some init data from the db and caches it <-- this works, btw.
//log4net.Config.XmlConfigurator.Configure();//not needed since we config in AssemblyInfo.cs
log4net.GlobalContext.Properties["UserName"] = "";//HttpContext.Current.User.Identity.Name does not exist yet
log4net.GlobalContext.Properties["UserId"] = "0";//so we can't get a user id, either...
log4net.ILog logger = log4net.LogManager.GetLogger("Global");
logger.Info("Application Start");//logs to event log but not to db...
}
protected void Session_Start(object sender, EventArgs e)
{
ulong UserId=GetUserId(HttpContext.Current.User.Identity.Name);//db call to get user id.
//log4net.Config.XmlConfigurator.Configure();//not needed since we config in AssemblyInfo.cs
log4net.GlobalContext.Properties["UserName"] = HttpContext.Current.User.Identity.Name;
log4net.GlobalContext.Properties["UserId"] = UserId;
log4net.ILog logger = log4net.LogManager.GetLogger("Global");
logger.Info("Session_Start");//logs to event log and db
}
public class CustomAdoNetAppender : AdoNetAppender
{
public string ConnectionStringName
{
set { ConnectionString = ConfigurationManager.ConnectionStrings[value].ToString(); }
}
}
有任何想法吗?