我有一个使用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。在配置文件中更改附加程序出现的顺序没有任何效果。
config文件中的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(); }
}
}
有什么想法吗?