log4net AdoNetAppender无法在Application_Start()中工作

时间:2012-07-20 11:20:27

标签: c# asp.net oracle11g log4net

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

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

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

尝试在visual studio中的所有异常中启用break来查看log4net中是否存在异常,它会捕获并吞下但可能会告诉您原因。

由于数据库用户登录错误而导致我没有登录到数据库,这对我有帮助。

答案 1 :(得分:0)

当你问dba是否有任何约束或触发器可能会在你输入空字符串时触发,并且他们向你保证没有,并且似乎没有其他合理的解释,请再次询问他们,同时站在他们的立方体让他们告诉你。

刚收到一封电子邮件,告诉我他们保证我不存在的约束确实存在......

为什么没有抛出异常我仍然不知道。