cab*_*ird 5 c# event-log custom-eventlog
我正在尝试写入应用程序事件日志。以下代码在 Windows 8 下执行时没有错误(以管理员权限运行时),但在 Windows 事件查看器中查看应用程序日志时,我没有看到任何事件显示。谁能帮我弄清楚我做错了什么。我需要在 app.config 中添加一些内容吗?
using System.Diagnostics;
namespace tracetest2
{
class Program
{
static void Main(string[] args)
{
if (!EventLog.SourceExists("TestSource"))
{
EventLog.CreateEventSource("TestSource", "Application");
}
EventLog appLog = new EventLog("Application", ".", "TestSource");
appLog.EnableRaisingEvents = true;
appLog.EndInit();
Debug.WriteLine(appLog.Entries.Count);
appLog.WriteEntry("An entry to the Application event log.");
Debug.WriteLine(appLog.Entries.Count);
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据微软网站,我们有以下信息:
注意:如果源已映射到日志并且您将其重新映射到新日志,则必须重新启动计算机才能使更改生效。
每次创建新的自定义事件密钥时都必须重新启动计算机。(或者只是重新启动 EventViewer 服务):)