2011-04-05 250 views
20

我有log4net安裝並配置爲插入到sql server 2005表。我的表被稱爲日誌。當我調用log4net方法時,它不會在sql server的日誌數據庫中輸入任何數據。我沒有從我的客戶端C#代碼中收到任何錯誤。我需要將用戶添加到sql中的日誌表嗎?現在我正在使用Windows身份驗證。Log4net沒有插入數據庫?

<configSections> 
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 
</configSections> 
<log4net> 
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender"> 
    <bufferSize value="100" /> 
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
connectionString value="Data Source=V-FIN-SQL-D\SQL2005;Initial Catalog=DevMHAIC;Integrated Security=True;Connection Timeout=360" /> 
    <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES 
     (@log_date, @thread, @log_level, @logger, @message, @exception)" /> 
<parameter> 
    <parameterName value="@log_date" /> 
     <dbType value="DateTime" /> 
<layout type="log4net.Layout.RawTimeStampLayout" /> 
    </parameter> 
    <parameter> 
<parameterName value="@thread" /> 
    <dbType value="String" /> 
    <size value="32" /> 
<layout type="log4net.Layout.PatternLayout"> 
<conversionPattern value="%t" /> 
</layout> 
</parameter> 
    <parameter> 
    <parameterName value="@log_level" /> 
    <dbType value="String" /> 
    <size value="512" /> 
    <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%p" /> 
    </layout> 
    </parameter> 
    <parameter> 
    <parameterName value="@logger" /> 
    <dbType value="String" /> 
    <size value="512" /> 
    <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%c" /> 
    </layout> 
    </parameter> 
    <parameter> 
    <parameterName value="@message" /> 
    <dbType value="String" /> 
    <size value="4000" /> 
    <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%m" /> 
    </layout> 
    </parameter> 
    <parameter> 
    <parameterName value="@exception" /> 
    <dbType value="String" /> 
    <size value="2000" /> 
    <layout type="log4net.Layout.ExceptionLayout" /> 
    </parameter> 
</appender> 
<root> 
    <level value="DEBUG" /> 
    <appender-ref ref="ADONetAppender" /> 
</root> 
</log4net> 

這裏是數據庫的日誌我的SQL代碼:

CREATE TABLE [dbo].[Log](
[Id] [int] IDENTITY(1,1) NOT NULL, 
[Date] [datetime] NULL, 
[Thread] [varchar](255) NULL, 
[Level] [varchar](50) NULL, 
[Logger] [varchar](255) NULL, 
[Message] [varchar](4000) NULL, 
[Exception] [varchar](2000) NULL 
) ON [PRIMARY] 

這裏是我的課,我使用調用log4net的。

public class ErrorLog : ILog 
{ 
    log4net.ILog _Log; 

    /// <summary> 
    /// Initializes a new instance of the <see cref="ErrorLog"/> class. 
    /// </summary> 
    /// <param name="log">The log.</param> 
    public ErrorLog(log4net.ILog log) : this() 
    { 
     _Log = log; 
    } 

    /// <summary> 
    /// Initializes a new instance of the <see cref="ErrorLog"/> class. 
    /// </summary> 
    public ErrorLog() 
    { 
     _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 

     log4net.Config.XmlConfigurator.Configure(); 
    } 

    /// <summary> 
    /// Informationals the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    public void informational(string message) 
    { 
     _Log.Info(message); 
    } 

    /// <summary> 
    /// Informationals the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    /// <param name="innerException">The innerException.</param> 
    public void informational(string message, Exception innerException) 
    { 
     _Log.Info(message, innerException); 
    } 

    /// <summary> 
    /// Debugs the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    public void debug(string message) 
    { 
     _Log.Debug(message); 
    } 

    /// <summary> 
    /// Debugs the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    /// <param name="innerException">The innerException.</param> 
    public void debug(string message, Exception innerException) 
    { 
     _Log.Debug(message, innerException); 
    } 

    /// <summary> 
    /// Errors the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    public void error(string message) 
    { 
     _Log.Error(message); 
    } 

    /// <summary> 
    /// Errors the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    /// <param name="innerException">The innerException.</param> 
    public void error(string message, Exception innerException) 
    { 
     _Log.Error(message, innerException); 
    } 

    /// <summary> 
    /// Warnings the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    public void warning(string message) 
    { 
     _Log.Warn(message); 
    } 

    /// <summary> 
    /// Warnings the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    /// <param name="innerException">The innerException.</param> 
    public void warning(string message, Exception innerException) 
    { 
     _Log.Warn(message, innerException); 
    } 
} 

這裏是我的接口:

public interface ILog 
{ 
    /// <summary> 
    /// Informationals the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    void informational(string message); 

    /// <summary> 
    /// Informationals the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    /// <param name="innerException">The innerException.</param> 
    void informational(string message, Exception innerException); 

    /// <summary> 
    /// Debugs the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    void debug(string message); 

    /// <summary> 
    /// Debugs the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    /// <param name="innerException">The innerException.</param> 
    void debug(string message, Exception innerException); 

    /// <summary> 
    /// Errors the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    void error(string message); 

    /// <summary> 
    /// Errors the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    /// <param name="innerException">The innerException.</param> 
    void error(string message, Exception innerException); 

    /// <summary> 
    /// Warnings the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    void warning(string message); 

    /// <summary> 
    /// Warnings the specified message. 
    /// </summary> 
    /// <param name="message">The message.</param> 
    /// <param name="innerException">The innerException.</param> 
    void warning(string message, Exception innerException); 
} 
+0

您可以發佈您log4net的配置文件? – taylonr 2011-04-05 18:13:12

回答

49
<bufferSize value="100" /> 

這是說,它將保持100個日誌在內存直到寫入DB。也許這就是爲什麼你在DB中看不到任何東西?

+0

這是最有可能的罪魁禍首。將該緩衝區設置爲1,並且每次都會寫入數據庫。實際上,它正在等待100個日誌條目被累積,然後纔會將它們寫入到數據庫中。 – IAmTimCorey 2011-05-03 16:41:14

+0

有沒有人知道這個值的默認值,如果它沒有指定? – 2013-02-25 19:53:58

+0

@MikeCole Docs說500,但爲什麼你還沒有在調試器中檢查過? – 2013-02-25 21:37:15

10

我真的沒有任何見解,您的問題只要看一眼你的配置,但你可以嘗試添加以下內容到你的應用程序設置,看是否log4net的可以告訴你什麼錯誤:

<appSettings>   
    <add key="log4net.Internal.Debug" value="true"/> 
</appSettings> 

見標題爲"How do I enable log4net internal debugging?"一節的如何捕獲的log4net的內部記錄的說明。

0

除非您在連接字符串中專門設置用戶,否則Log4Net將嘗試使用分配給該進程的任何用戶帳戶進行登錄(如果是這種情況,則爲ASP.NET工作進程)。如果您無法登錄,Log4Net也不會拋出異常。您可以打開調試以查看問題所在。

+0

如何開啓調試功能? – barrypicker 2016-05-31 20:54:21

+0

@barrypicker hbulens 2016-09-06 12:28:23

1

我有同樣的問題。就我而言,我不得不從插入中刪除[Exception]和@exception的使用。因此,我不得不改變我的日誌記錄。我基本上必須創建一個很好的轉儲異常並將其傳遞給消息參數。

我希望這有助於