写入事件日志 Windows 计划任务

发布于 2024-08-01 17:44:29 字数 210 浏览 4 评论 0 原文

我有一个控制台应用程序,它将通过计划任务运行,我想做的是将其写入 catch 块中的事件日志。 我尝试过使用

EventLog.WriteEntry("My App Name","Error Message - " ex.ToString() );

,但由于某种原因它没有写入错误。 难道我做错了什么?

谢谢

I have a console app that will be running through a scheduled task and what i'd like to do is have it write to an Event Log in a catch block. I've tried using

EventLog.WriteEntry("My App Name","Error Message - " ex.ToString() );

but for some reason it is not writing the error.
Am i doing something wrong?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜宝宝 2024-08-08 17:44:29

您需要确保事件源存在,例如:

if (!EventLog.SourceExists("MySource"))
    EventLog.CreateEventSource("MySource","Application");

请参阅 https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.eventlog.createeventsource?view=net-8.0

You need to make sure the event-source exists, e.g.:

if (!EventLog.SourceExists("MySource"))
    EventLog.CreateEventSource("MySource","Application");

See https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.eventlog.createeventsource?view=net-8.0

携余温的黄昏 2024-08-08 17:44:29

需要注意的一件事是,调用 EventLog.CreateEventSource 时有时会出现轻微延迟,因此在创建后立即尝试访问创建的 EventSource 时应该注意这一点。

One thing to note is that there is sometimes a small delay when calling EventLog.CreateEventSource, so you should be aware of that when trying to access the created EventSource immediately after creation.

深巷少女 2024-08-08 17:44:29

这段代码来自MSDN网站的C#,希望对你有帮助。

using System;
using System.Diagnostics;
using System.Threading;

class MySample{

    public static void Main(){

        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource")){
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatingEventSource");
        }

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";

        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");

    }
}

This code is from MSDN website in C#, I hope it help you.

using System;
using System.Diagnostics;
using System.Threading;

class MySample{

    public static void Main(){

        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource")){
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatingEventSource");
        }

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";

        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文