【发布时间】:2012-10-01 07:28:14
【问题描述】:
我刚刚在我的 C# WinForms 应用程序中设置了 log4net,方法是添加引用、将配置添加到 App.config 以及在 AssemblyInfo.cs 中加载条目。配置设置为捕获所有级别。
在我的 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);
}
}
}
我将“this works”放入只是为了测试我是否可以实际写入日志文件,这就是日志中显示的内容:
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
我也希望在日志中看到第二个错误,但没有任何迹象:(
根据 Keith Nicholas 的评论,我这样做了:
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
【问题讨论】: