gpt4 book ai didi

c# - 到达代码,不执行并无错误退出

转载 作者:行者123 更新时间:2023-11-30 15:02:20 25 4
gpt4 key购买 nike

到目前为止,我的所有代码都可以正常工作:

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);
}
}
}

我什至在另一个项目中使用了这个类并且它工作正常。当它碰到这条线时:

如果(!EventLog.SourceExists(来源)) EventLog.CreateEventSource(源, 日志);

它命中这条线并退出。

这是我的输出:

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,但没有帮助。我在打开所有错误时都有提示。

我错过了什么?

最佳答案

首先我会用大括号括起你的条件,因为如果 EventLog 没有源,EventLog 是否应该写入条目有点不清楚。

if (!EventLog.SourceExists(source)) 
{
EventLog.CreateEventSource(source, log);
}

EventLog.WriteEntry(source, message, eventLogEntryType, eventId);

也尝试将它包装在 try/catch block 中,看看是否有任何未处理的异常导致程序随机退出。

try {

if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, log);
}

EventLog.WriteEntry(source, message, eventLogEntryType, eventId);

} catch (Exception e)
{
Console.WriteLine(e);
}

附加:线程 'vshost.NotifyLoad' (0x28c) 已退出,代码为 0 (0x0)。线程 'vshost.LoadReference' (0x470) 已退出,代码为 0 (0x0)。不是错误。 Visual Studio 告诉您后台线程已退出。 0表示线程运行成功。

关于c# - 到达代码,不执行并无错误退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12803640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com