使用事件日志启用多个源将条目写入同一日志
我编写了两个作为 Windows 服务运行的不同应用程序。每个应用程序代表 Windows 日志的一个来源。 1.) 问题: 我希望应用程序将条目写入同一日志,例如 my_applications_log。我的动机是监视应用程序所采取的操作的顺序。问题是,当我尝试将一个源的条目写入同一日志,然后将另一个源的另一个条目写入同一日志时,只有注册了最新源的条目才会写入日志,而另一个则不会。我不明白为什么这是不可能的。我知道一种方法是根据来源分割日志并使用自定义视图将它们“加入”。但我想要一个指定的日志,而不是两个或更多..
2.) 问题: 在Windows事件查看器中,我可以看到日志可以组织到“目录”中,例如:应用程序和服务日志->Microsoft->Windows->音频->{CaptureMonitor日志,操作日志}。我找不到任何允许创建此类目录以及此目录中的一些日志的 API。这有可能吗?
提前致谢
I have written two different applications running as windows services. Each application represents one source to the windows logs.
1.) Question:
I want the applications to write entries to the same log e.g. my_applications_log. My motivation is to monitor order of the actions taken by the applications. Problem is that when I try to write an entry with one source and then another entry with another source to the same log, only the entry with the latest source registered is written to the log, the other one not. I don't understand why this should not be possible.. I know one way is to split logs according to sources and use custom view to have them "joined". But I would like to have one specified log not two or more..
2.) question:
In Windows Event Viewer I can see that logs can be organized into "directories" e.g.: Application and Services Logs->Microsoft->Windows->Audio->{CaptureMonitor log, Operational log} . I can not find any API allowing to create such a directory and the some logs within this directory. Is this possible somehow??
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试跨进程共享日志文件,则需要使用某种锁来保护该文件,或者在写入失败时让应用程序重试。两个进程不可能同时写入同一个文件。两个进程可以打开文件进行写入,但如果它们都尝试同时写入,那么您可能会遇到异常。
也许最简单的方法是使用互斥体。每个应用程序都会在启动时使用相同的名称创建一个命名的
Mutex
:然后,当您想要写入该文件时:
如果您想要写入 Windows 事件日志,请查看 System.Diagnostics.EventLog 类。我没有任何写入 Windows 事件日志的经验,所以我不能说这是否适合您。
If you're trying to share a log file across processes, you need to protect that file with a lock of some kind, or have the applications retry if a write fails. It's not possible for two processes to be writing to the same file at the same time. Two processes can have the file open for writing, but if they both try to write at the same time, then you'll likely get an exception.
Probably the easiest way to do this is with a
Mutex
. Each application would create a namedMutex
at startup, using the same name:Then, when you want to write to the file:
If you want to write to the Windows event log, look into the System.Diagnostics.EventLog class. I don't have any experience with writing to the Windows event log, so I can't say whether that will work for you.