达code,不执行和退出,没有错误 [英] Reaches code, doesn't execute and exits without error
问题描述
我所有的code正常工作了这一点:
All my code works fine up to this point:
using System.Diagnostics;
namespace WebPortalLogging
{
public static class EventLogging
{
public static void LogEvent(string origin, string message, EventLogEntryType eventLogEntryType, int eventId)
{
const string source = "Software";
const string log = "Application";
if (!EventLog.SourceExists(source))
EventLog.CreateEventSource(source, log);
EventLog.WriteEntry(source, message, eventLogEntryType, eventId);
}
}
}
我甚至使用这个类中的其他项目,它工作正常。当它击中这一行:
I even use this class in another project and it works fine. When it hits this line:
如果(!EventLog.SourceExists(源)) EventLog.CreateEventSource(源,日志);
if (!EventLog.SourceExists(source)) EventLog.CreateEventSource(source, log);
它击中这一行并退出。
下面是什么东西在我的输出:
Here is what is in my output:
The thread 'vshost.NotifyLoad' (0x28c) has exited with code 0 (0x0).
The thread 'vshost.LoadReference' (0x470) has exited with code 0 (0x0).
'VmBackup.vshost.exe' (Managed (v4.0.30319)): Loaded 'D:\Google Drive\Code\VMBackup\VMBackup\bin\Debug\VmBackup.exe', Symbols loaded.
'VmBackup.vshost.exe' (Managed (v4.0.30319)): Loaded 'D:\Google Drive\Code\VMBackup\VMBackup\bin\Debug\WebPortalLogging.dll', Symbols loaded.
The thread '<No Name>' (0xa44) has exited with code 0 (0x0).
'VmBackup.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread '<No Name>' (0x107c) has exited with code 0 (0x0).
The thread '<No Name>' (0x1838) has exited with code 0 (0x0).
The thread 'vshost.RunParkingWindow' (0xa78) has exited with code 0 (0x0).
The thread '<No Name>' (0x10e0) has exited with code 0 (0x0).
The program '[6436] VmBackup.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[6436] VmBackup.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
有未到达EventLog.WriteEntry。它不输出到事件日志任。我重新启动VS2010,并没有帮助。我已经提示,在所有的错误开启。
It doesn't reach the EventLog.WriteEntry. It is not outputting to the event log either. I've restarted VS2010 and that didn't help. I have prompt at all errors turned on.
我在想什么?
推荐答案
首先,我会用括号圆你的条件,因为它是一个有点不清楚,如果事件日志应该写的条目,事件日志可是没有一个来源。
Firstly I would use braces round your conditional as it is a little unclear if EventLog should Write the entry if EventLog doesnt have a Source.
if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, log);
}
EventLog.WriteEntry(source, message, eventLogEntryType, eventId);
也可以尝试,把它包在的try / catch
块,看看你得到任何未处理的异常是程序导致随机退出。
Also try and wrap it in an try/catch
block to see if you get any unhandled exception which is causing the program to randomly exit.
try {
if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, log);
}
EventLog.WriteEntry(source, message, eventLogEntryType, eventId);
} catch (Exception e)
{
Console.WriteLine(e);
}
其它:线程vshost.NotifyLoad(0x28c)已退出与code 0(为0x0)
。
线程vshost.LoadReference(0x470)已退出与code 0(为0x0)
。不是错误。 Visual Studio是告诉你,一个后台线程已退出。 0表示线程运行成功。
Additional: The thread 'vshost.NotifyLoad' (0x28c) has exited with code 0 (0x0)
.
The thread 'vshost.LoadReference' (0x470) has exited with code 0 (0x0)
. are not errors. Visual Studio is telling you that a background thread has exited. 0 indicates the thread ran successfully.
这篇关于达code,不执行和退出,没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!