- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有充当同步软件的窗口服务。我想在我的服务上添加未处理的异常日志记录,所以我修改了我的 program.cs
如下:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
static void Main()
{
// Register Unhandled Exception Handler
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(UnhandledExceptionHandler);
// Run Service
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service()
};
ServiceBase.Run(ServicesToRun);
}
static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
// Get Exception
Exception ex = (Exception)args.ExceptionObject;
// Generate Error
string ErrorMessage = String.Format(
"Error: {0}\r\n" +
"Runtime Terminating: {1}\r\n----- ----- ----- ----- ----- -----\r\n\r\n" +
"{2}\r\n\r\n####################################\r\n",
ex.Message,
args.IsTerminating,
ex.StackTrace.Trim());
// Write Error To File
try
{
using (StreamWriter sw = File.AppendText("UnhandledExceptions.log"))
sw.WriteLine(errorMessage);
}
catch { }
}
}
然后在我的 Service.cs
文件中,在 OnStart
方法中,我添加了一个 throw new Exception("test");
到查看未处理的异常是否按预期记录到文件中。
当我启动我的服务时,它立即停止如预期;但是它似乎没有将异常记录到指定的文件。
知道我在这里做错了什么吗?在此先感谢您的帮助。
在你询问之前,我的服务作为 Local Service
运行,并且我的服务 .exe 运行所在的目录 (c:\mysync) 已经有 Local Service
添加在具有完全读/写访问权限的安全选项卡中。
最佳答案
OnStart 在 try-catch block 内的服务基类中调用。如果在此阶段发生异常,它会捕获它并将状态设置为 1 并且不会进一步抛出它:
string[] args = (string[]) state;
try
{
this.OnStart(args);
.....
}
catch (Exception ex)
{
this.WriteEventLogEntry(Res.GetString("StartFailed", new object[1]
{
(object) ((object) ex).ToString()
}), EventLogEntryType.Error);
this.status.currentState = 1;
}
因此,您可以在 EventLogs 中找到一条记录,但您无法将其捕获为未处理的域异常,因为不存在此类异常。
关于c# - 在 Windows 服务中处理 AppDomain.CurrentDomain.UnhandledException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23114479/
所以我的问题围绕着节省内存。 本质上,我需要将程序集加载到主域/当前域以外的单独应用程序域中,检查该程序集中的类型,然后在完成后卸载新域。 目前我的解决方案如下: AppDomain NewDomai
我想枚举 Asp.NET 应用程序中所有加载的程序集,使用 AppDomain.CurrentDomain.GetAssemblies() .但是,在检查 AppDomain 的文档时,我发现以下语句
当我需要在关闭应用程序之前使用 Stop() 函数停止后台线程时,我有一个简单的应用程序。问题是我的 Main() 函数有几个退出点(返回语句) static void Main(string[] a
我有一个带有绑定(bind)到 Application.CurrentDomain.UnhandledException 的事件处理程序的 .NET 程序。 运行带有调试的程序时,当抛出未处理的异常时
我有一个全局异常处理程序,可以在Main.cs中很好地工作,但是在捕获到错误后,我的应用程序不存在了,而只是挂起了。我尝试了Environment.Exit,退出选择器,返回,重新抛出异常-所有这些仍
我目前正在使用以下代码: AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { var name = args.Name
我想在发生未处理的异常时在问题跟踪器系统中创建错误报告。由于这是通过一系列异步http请求完成的,所以对应的方法是async return a task: AppDomain.CurrentDomai
我在单独的类库项目中有电子邮件模板(和电子邮件发送器)。我正在获取 View 路径(复制到输出目录 = true),如下所示: var basePath = AppDomain.CurrentDoma
在 VS2012 中,我有一堆 (MS) 单元测试,它们使用我存储在与测试文件类相同的文件夹中的不同 xml/文本文件。 我使用 File.OpenRead(AppDomain.CurrentDoma
我有一个程序集,当访问它时,它会启动一个线程来处理放置在队列中的项目。在该程序集中,我将一个处理程序附加到 DomainUnload 事件: AppDomain.CurrentDomain.Domai
我想遍历我在项目中添加的所有类 Assembly[] foo = AppDomain.CurrentDomain.GetAssemblies(); foreach(Assembly a in foo)
我有一个 WCF 服务,在 Global.asax 中有以下代码: protected void Application_Start(object sender, EventArgs e) {
我正在尝试使用 CurrentDomain.AssemblyResolve 事件来加载标记为嵌入式资源的 DLL。具体来说,我的问题来自于我试图将程序集用作子类的事实,如下所示: #define BR
我试图根据答案实现解决方案 How to handle exceptions raised in other threads when unit testing? ,但我仍然不明白在处理程序中做什么。
我已经设置了 UnhandledException 以在我的服务崩溃之前记录任何异常。 AppDomain.CurrentDomain.UnhandledException += new Unhand
在找到关于程序集解析 (AppDomain.CurrentDomain.AssemblyResolve asking for a .resources assembly?) 的最后一个问题的答案后,我
如何将 AppDomain.CurrentDomain.BaseDirectory 的相对路径获取到 Uri 中。 我需要从 AppDomain.CurrentDomain.BaseDirectory
总的来说,我试图从主域中的 dll 执行方法,但之后,卸载该 dll。到目前为止,我已经创建了新的 AppDomain 并在那里加载了 Assembly\dll,使用 MarshalByRefObje
我遇到了一个问题,我的单元测试没有在我的 TFS 构建机器上运行,尽管它们正在我的开发人员机器上运行。 我在以下行中遇到异常,试图为特定接口(interface)加载所有加载的程序集: var
我在使用 ReSharper (8.2.2) 和 VS 2013 运行我的测试用例时遇到了一个奇怪的错误。 演示该问题的简化测试用例仅包含两行代码: CallContext.LogicalSetDat
我是一名优秀的程序员,十分优秀!