gpt4 book ai didi

c# - log4net AdoNetAppender 在 Application_Start() 中不工作

转载 作者:行者123 更新时间:2023-11-30 18:38:19 26 4
gpt4 key购买 nike

有一个 asp.net、dot net 4 网络应用程序,使用 log4net 进行日志记录。我们记录到 Windows 事件日志和 Oracle 11g 表。除了Application_Start之外,我们可以在任何地方登录到oracle表。真正烦人的事情是什么?它默默地失败了。打开控制台日志记录,并重定向到一个文件,在我们开始记录之前的最后一个条目是“将名为 [AdoNetAppender] 的附加程序添加到记录器 [root]”。 Repository.Configured(对于 log4net-default-repository)此时对所有记录器都是正确的。反转附加程序在配置文件中出现的顺序无效。

配置文件的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(); }
}
}

有什么想法吗?

最佳答案

尝试在 visual studio 中打开所有异常的中断,看看 log4net 中是否抛出异常,它会捕获并吞下但可能会告诉您原因。

这在过去因为数据库用户登录错误而没有登录数据库时对我有帮助。

关于c# - log4net AdoNetAppender 在 Application_Start() 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11578291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com