2012-09-30 25 views
1

我剛剛在我的C#WinForms應用程序中設置了log4net,方法是添加引用,將配置添加到App.config和AssemblyInfo.cs中的加載項。該配置設置爲捕捉所有級別。Program.cs中的log4net

在我的Program.cs中,我試圖讓它捕獲每一個錯誤。

目前,我有這樣的:

static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     private static readonly ILog log = LogManager.GetLogger(typeof(Program)); 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      try 
      { 
       log.Info("this works"); 
       Application.Run(new Forms.Main()); 
      } 
      catch (Exception e) 
      { 
       log.Info("nothing here", e); 
      } 
     } 
    } 

我把「這部作品」在短短的測試,我其實可以寫入日誌文件,這是什麼在日誌中顯示出來:

2012-09-30 23:00:53,959 [INFO ] - this works 

我log4net的是還設置了寫入立即窗口,所以我創建的目的一些錯誤,這就是我在窗口中看到:

ContractManagement.Program: 2012-09-30 23:08:09,177 [INFO ] - this works 
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll 

我本來期望看到日誌中的第二個錯誤還有,但未見其蹤影:(


繼基思·尼古拉斯的評論,我已經做到了這一點:

static void Main() 
{ 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 

    log.Info("this works"); 
    throw new System.ArgumentException("Keith Nicholas Test"); 
    //Application.Run(new Forms.Main()); 

} 

和日誌文件顯示:

2012-09-30 23:19:12,090 [INFO ] - this works 
System.ArgumentException: Keith Nicholas Test 
    at ContractManagement.Program.Main() in c:\ContractManagement\ContractManagement\Program.cs:line 25 

回答

0

如果異常是從一個新的線程拋出,它不會通過catch塊捕獲....

只是爲了證明你的日誌記錄工作確定: -

,而不是運行應用程序,只是拋出一個新的異常,第一log.Info之後證明自己的異常記錄的作品。

,如: -

try 
{ 
    log.Info("this works"); 
    throw new System.ArgumentException("Keith Nicholas Test"); 
} 
catch (Exception e) 
{ 
    log.Info("nothing here", e); 
} 
+0

請看看我的編輯:) – hshah

+1

請看看我的編輯:) –

+0

那在日誌中給出了以下內容:2012-10-01 00:08:02,127 9 ContractManagement.Program [INFO] - 此作品 2012-10-01 00:08:02,136 9 ContractManagement.Program [INFO] - 這裏沒什麼 系統.ArgumentException:Keith Nicholas Test at ContractManagement.Program.Main()in c:\ ContractManagement \ ContractManagement \ Program.cs:line 26 – hshah

1
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll 

這不是你的log4net的配置有問題。其中一個內部庫引發了一個異常,然後捕獲它,所以它從來沒有將它寫入你的catch子句。開發環境直接向直接窗口寫出了有關被捕獲異常的信息。

是log4net正在寫入直接窗口,但並非立即窗口中的所有內容都來自log4net。

[更新 - 以反映登錄多個線程更普遍的問題]

雖然不直接關係到你的問題,有時已儀表與log4net的日誌類,可以在同一時間在不同的線程中執行。您可能會同時從多個線程中獲取調試,信息和/或錯誤方法。建議您更新App.config中的消息模式以包含該線程,以便您可以跟蹤哪條消息來自哪個線程。例如:

<conversionPattern value="%level %thread %logger - %message%newline" /> 

這只是一個例子 - 你沒有包括轉換模式,所以我不知道你會看什麼樣,如果你要添加%線程。

+0

因此,如果另一個線程導致錯誤,我該如何讓log4net抓住它呢?我不特別想在整個應用程序中添加日誌條目。 – hshah

+1

對不起,如果我不清楚。我已經重寫了該部分的答案。我並不是建議你添加一堆catch語句,而只是對你的配置文件做一些小修改。 –

+0

對不起,看起來我不清楚。我帶上了你所說的話並更新了我的配置,但我的評論是關於原始問題和答案的第一段。 – hshah

0

很多後來谷歌搜索的,我有:

static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    private static readonly ILog log = LogManager.GetLogger(typeof(Program)); 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 

     AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 
     Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); 
     AppDomain.CurrentDomain.FirstChanceException += FirstChanceHandler; 

     Application.Run(new Forms.Main()); 
    } 

    private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) 
    { 
     log.Info(e.Exception.Message); 
    } 

    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
    { 
     log.Info(e); 
    } 

    static void FirstChanceHandler(object source, FirstChanceExceptionEventArgs e) 
    { 
     log.Info(e.Exception.Message); 
    } 
} 

這捕獲我希望能捕捉FirstChanceExceptions:

2012-10-01 00:24:02,532 9 ContractManagement.Program [INFO ] - The database file cannot be found. Check the path to the database. [ Data Source = C:\ContractManagement\ContractManagement\bin\Debug\ContractManagement.sdf ]