【发布时间】:2017-06-20 06:40:10
【问题描述】:
我正在开发 Windows 服务、C#、4.7 .NET Framework 和 log4net。
我想记录未处理的异常,但似乎 log4net 在Program.cs 中不起作用。
using System;
using System.ServiceProcess;
namespace MyWindowsService
{
static class Program
{
private static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
log4net.Config.XmlConfigurator.Configure();
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
ServiceBase.Run(ServicesToRun);
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
log.ErrorFormat("UnhandledException: {0}", e.ToString());
}
}
}
我已修改 MyService 类以在 OnStart 方法上引发异常,但我没有在日志文件中获得任何日志,也无法调试 Windows 服务应用程序以查看是否调用了 CurrentDomain_UnhandledException。
您知道如何记录未处理的异常吗?
【问题讨论】:
-
这个answer 有用吗?
标签: c# windows-services log4net