我剛剛在我的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
請看看我的編輯:) – hshah
請看看我的編輯:) –
那在日誌中給出了以下內容: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