在 C# 中写入事件日志 - 写入应用程序日志时是否需要使用 EventLog.CreateEventSource?

发布于 2024-12-13 06:39:34 字数 586 浏览 2 评论 0原文

当我使用以下代码写入应用程序事件日志时,一切正常:

EventLog log = new EventLog();
log.Source = "Application";
log.WriteEntry("test message", EventLogEntryType.Error);

当我使用来自 MSDN 和所有其他博客的代码时,我收到安全错误(我猜测是因为 CreateEventSource 引发了它)。

string sSource = "MyWebService";
string sLog = "myApplication";
string sMsg = errorMessage;

if (!EventLog.SourceExists(sSource))
   EventLog.CreateEventSource(sSource, sLog);

 EventLog.WriteEntry(sSource, sMsg, EventLogEntryType.Error);  

那么,如果我只需要写入默认情况下存在的应用程序日志,我是否需要检查源是否存在?

写入事件查看器的正确方法是什么?

When I use the following code to write to Application Event log, everything works fine:

EventLog log = new EventLog();
log.Source = "Application";
log.WriteEntry("test message", EventLogEntryType.Error);

When I use the code that is from MSDN and all other blogs, I get the security error (I am guessing because CreateEventSource raises it).

string sSource = "MyWebService";
string sLog = "myApplication";
string sMsg = errorMessage;

if (!EventLog.SourceExists(sSource))
   EventLog.CreateEventSource(sSource, sLog);

 EventLog.WriteEntry(sSource, sMsg, EventLogEntryType.Error);  

So, do I need to check whether the source exists if all I need is to write to Application log which is there by default?

What is the proper way to write to EventViewer?

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

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

发布评论

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

评论(6

以可爱出名 2024-12-20 06:39:34

CreateEventSource 方法在事件日志中创建一个新源,这允许您在应用程序自己的组中写入应用程序的日志,而不是在通用 Application 组中写入。

也许您收到错误是因为您用来创建事件源的用户没有创建事件源的权限,如果您在 Vista/7 操作系统下,请尝试以管理员身份运行您的程序。

登录事件查看器的正确方法取决于您的需求,如果您的应用程序生成大量日志消息并且您希望将此日志分组到应用程序特定的容器中,也许最好创建一个应用程序特定的日志事件源并写入相反,如果您的应用程序生成很少的日志消息并且无需将它们分组在一起,您可以使用通用的 Application 日志事件源...

The CreateEventSource method create a new source in the event log, this allow you to write log of your application in the application own group instead of writing in the generic Application group.

Maybe you get an error because the user that you using to create the event source doesn't have the permission to create it, try to run your program as administrator if you are under Vista/7 OS.

The proper way to log in the event viewer depends on your needs, if your application generates a lot of logging message and you want to group this log in an application specific container, maybe it is better to create an application specific log event source and write the log in it, instead if your application generates few log messages and there is no need to group them together you can use the the generic Application log event source ...

拥抱我好吗 2024-12-20 06:39:34

我建议您尝试 log4net,以防您也想写入不同的源(smtp、文件等)

http://logging.apache.org/log4net/release/config-examples.html#eventlogappender

对于 Web 应用程序:

一般用途:

winforms/windows 服务的类似解决方案。

I suggest you try log4net, in case you want to write to different sources as well (smtp, file, etc.)

http://logging.apache.org/log4net/release/config-examples.html#eventlogappender

For web apps:

General use:

Similar solution for winforms/windows service.

似最初 2024-12-20 06:39:34

您需要具有管理员权限才能创建事件源。在第一个中,您没有使用自定义源。

You need to have admin rights to create an event source. In the first one you are not using a custom source.

多情癖 2024-12-20 06:39:34

直接的 WriteEntry 将转到默认的应用程序源。如果您想创建自己的自定义源,则可以使用 SourceExists 和 CreateEventSource,这样可以更轻松地在事件查看器中找到任何日志条目。

是的,您需要有权创建客户事件源,正如其他人提到的那样。

A straight WriteEntry will go to the default Application source. The SourceExists and CreateEventSource is if you want to create your own custom source which will be easier to locate any log entries in the Event Viewer.

And yes you need to have rights to create a customer event source as others have mentioned.

杯别 2024-12-20 06:39:34

您需要管理员权限才能运行您的应用程序。

您可以通过以下方式运行您的应用程序
进入应用程序的调试文件夹,右键单击 .exe 文件并以管理员身份运行

以管理员身份运行 Visual studio

You need admin rights to run your application.

Either you can run your application by
Going into the debug folder of your application and right click on your .exe file and run as admin

or

You run Visual studio as admin

别念他 2024-12-20 06:39:34

您不需要创建事件源。当生成独立于语言或具有替换的事件时,它可能是一个很大的优势,但它是可选的,至少对于 .NET 程序而言(BCL 提供默认事件源)。

You don't need to create an event source. It can be a big advantage when generating events that are language-independent or that have substitutions, but it's optional, at least for .NET programs (the BCL provides a default event source).

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