如何写入自定义事件日志?

发布于 2024-09-02 09:37:22 字数 707 浏览 1 评论 0原文

我正在尝试让我的 .Net Windows 服务正确访问自定义事件日志。我使用 EventLogInstaller 在安装应用程序时创建事件日志和源。我在此处读到 Windows 需要一段时间才能注册源因此他们建议您在尝试写入日志之前重新启动应用程序。

由于这是一项 Windows 服务,我不想强​​制计算机重新启动或让用户手动启动该服务,因此我使用此代码等待日志存在,然后自动启动该服务。

while (!(EventLog.Exists("ManageIT") || EventLog.SourceExists("ManageIT Client Service")))
{
    Thread.Sleep(1000);
}

System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("ManageIT.Client.Service");
controller.Start();

我的问题是,来自服务的事件仍然写入应用程序日志,尽管我可以在注册表编辑器中看到自定义日志,但它不会显示在 Windows 7 事件查看器中。

任何帮助将不胜感激。

I'm trying to get my .Net Windows Service to right to a custom event log. I'm using EventLogInstaller to create the event log and source when the application is installed. I read here that it takes a while for Windows to register the source so they reccomend you restart the application before trying to write to the log.

As this is a Windows Service I didn't want to have to force a computer restart or get the user to manually start the service up, so I use this code to wait for the log to exist and then start the service automatically.

while (!(EventLog.Exists("ManageIT") || EventLog.SourceExists("ManageIT Client Service")))
{
    Thread.Sleep(1000);
}

System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("ManageIT.Client.Service");
controller.Start();

My problem is that events from the service are still written to the Application Log and although I can see my custom log in the Registry Editor it does not show up in the Windows 7 Event Viewer.

Any help will be much appreciated.

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

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

发布评论

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

评论(4

乞讨 2024-09-09 09:37:22

默认情况下,安装服务后,源会与应用程序日志关联。
如果我们稍后更改此关联,系统需要重新启动。

但是,我们可以通过在服务类(继承自 servicebase 的类)构造函数中将 autolog 属性设置为 false 来阻止服务与应用程序日志关联。
http://msdn.microsoft.com/en-us /library/system.serviceprocess.servicebase.autolog.aspx

By default when a service is installed, the source gets associated with the Application Log.
If we change this association at a later point, the system needs a restart.

We can however prevent the association of the service with the application log, by setting autolog property to false in the service class (class which inherits from servicebase) constructor.
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.autolog.aspx

梦过后 2024-09-09 09:37:22

试试这个片段:

编辑 - 警告:如果运行代码的用户没有管理员权限,这将引发异常。由于这种情况(并且如果用户没有这些权限),最佳实践应该是假设日志存在,然后简单地写入它。请参阅:未找到来源,但无法搜索到部分或全部事件日志

if (!EventLog.SourceExists("MyApplicationEventLog"))
{
    EventSourceCreationData eventSourceData = new EventSourceCreationData("MyApplicationEventLog", "MyApplicationEventLog");
    EventLog.CreateEventSource(eventSourceData);
}

using (EventLog myLogger = new EventLog("MyApplicationEventLog", ".", "MyApplicationEventLog"))
{
    myLogger.WriteEntry("Error message", EventLogEntryType.Error);
    myLogger.WriteEntry("Info message", EventLogEntryType.Information);
}

Try this snippet:

edit - caveat: if the user running the code does not have administrator rights, this will throw an exception. Since this is the case (and if the user will not have these rights) best practices should be to assume the log exists, and simply write to it. see: The source was not found, but some or all event logs could not be searched

if (!EventLog.SourceExists("MyApplicationEventLog"))
{
    EventSourceCreationData eventSourceData = new EventSourceCreationData("MyApplicationEventLog", "MyApplicationEventLog");
    EventLog.CreateEventSource(eventSourceData);
}

using (EventLog myLogger = new EventLog("MyApplicationEventLog", ".", "MyApplicationEventLog"))
{
    myLogger.WriteEntry("Error message", EventLogEntryType.Error);
    myLogger.WriteEntry("Info message", EventLogEntryType.Information);
}
扶醉桌前 2024-09-09 09:37:22

听起来您正在像这样写入事件日志:

 EventLog.WriteEntry("Source", "Message");

这将写入应用程序日志。

如果您使用 simons post 中的代码创建 myLogger,则可以指定 Log 的名称。

It sounds like you are writing to the event log like this:

 EventLog.WriteEntry("Source", "Message");

This will write to the application log.

If you use the code in simons post with the creation of myLogger, you can specify the name of the Log.

旧夏天 2024-09-09 09:37:22

我做了这样的事情:

        var logName = EventLog.LogNameFromSourceName("MyApp", Environment.MachineName);

         //delete the source if it associated with the wrong Log
        if (!string.IsNullOrEmpty(logName) & logName != "MyLog")
        {
            EventLog.DeleteEventSource("MyApp", Environment.MachineName);
        }

        if (!EventLog.SourceExists("MyApp"))
        {
            EventLog.CreateEventSource("MyApp", "MyLog");
        }

I did something like this:

        var logName = EventLog.LogNameFromSourceName("MyApp", Environment.MachineName);

         //delete the source if it associated with the wrong Log
        if (!string.IsNullOrEmpty(logName) & logName != "MyLog")
        {
            EventLog.DeleteEventSource("MyApp", Environment.MachineName);
        }

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