windows 将事件日志组织到文件夹中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6386463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Organizing eventlogs into folders
提问by Tomas
I want to create multiple services and I want them to log each in a log entry under the same directory/folder that I specify so when I open Windows Event Viewer I can see them all placed in one folder. For example service1 would log into service1_log, service2 would log into service2_log and both service1_log and service2_log would reside in one folder named Myservices. I haven't found anything in Windows Event Log API or any other API that would fit into this problem..I can see in windows event viewer there are folders but I can only create logs into the root of the logs hierarchy there.
我想创建多个服务,并且我希望它们将每个服务都记录在我指定的同一目录/文件夹下的日志条目中,因此当我打开 Windows 事件查看器时,我可以看到它们都放在一个文件夹中。例如,service1 将登录到 service1_log,service2 将登录到 service2_log,并且 service1_log 和 service2_log 将驻留在名为 Myservices 的文件夹中。我没有在 Windows 事件日志 API 或任何其他适合此问题的 API 中找到任何内容..我可以在 Windows 事件查看器中看到有文件夹,但我只能在那里的日志层次结构的根中创建日志。
Thanks in advance Tomas
提前致谢 托马斯
回答by Martin Liversage
Windows Event Log does not log into directories. However, the event log is split into several logs (Application, System, Securityetc.) that are presented as folders in Event Viewer. You can create your own log that will appear as a separate folder in Applications and Services Logs(assuming Windows Vista or later) by calling EventLog.CreateEventSource
.
Windows 事件日志不会登录到目录。但是,事件日志被拆分为多个日志(应用程序、系统、安全等),在事件查看器中显示为文件夹。您可以创建自己的日志,将显示为一个单独的文件夹的应用程序和服务日志通过调用(假设Windows Vista或更高版本)EventLog.CreateEventSource
。
However, I will not recommend that you create your own log. Instead you can let your services log to the Applicationlog. By letting each service use a separate event source you can easily filter the Applicationlog to only only display the interesting log messages. Again assuming Windows Vista or later you can create a custom view below the Custom Viewsnode and filter it by source. This folder will then only show log messages from your services.
但是,我不建议您创建自己的日志。相反,您可以让您的服务登录到应用程序日志。通过让每个服务使用单独的事件源,您可以轻松过滤应用程序日志以仅显示有趣的日志消息。再次假设 Windows Vista 或更高版本,您可以在自定义视图节点下创建自定义视图并按源过滤它。然后,此文件夹将仅显示来自您的服务的日志消息。
Fast forward five years: Now, it is quite easy to create you own log that can be seen in Application and Services Logs
by using the EventSource
class which allows you to create events for Event Tracing for Windows (ETW). ETW is highly configurable, has very good performance but can also be a bit difficult to use initially. Message size is limited, installing a new manifest requires administrative rights, while channels like Diagnosticsare collecting data you can't view the log etc. But all in all ETW is a good alternative to logging to the Applicationevent log.
快进五年:现在,Application and Services Logs
通过使用EventSource
允许您为 Windows 事件跟踪 (ETW)创建事件的类,可以很容易地创建自己的日志。ETW 是高度可配置的,具有非常好的性能,但最初使用起来也有点困难。消息大小有限,安装新清单需要管理权限,而诊断等渠道正在收集数据,您无法查看日志等。但总而言之,ETW 是记录到应用程序事件日志的一个很好的替代方案。
回答by VoteCoffee
Long story short, it is not easily accomplished using the .NET 4 framework. It looks like .NET 4.5 offers better support for this, but is is not terribly simple. You can put them into folders per application, but putting those folders in a common folder would be a lot more complex.
长话短说,使用 .NET 4 框架并不容易实现。看起来 .NET 4.5 对此提供了更好的支持,但并不是非常简单。您可以将它们放入每个应用程序的文件夹中,但将这些文件夹放入公共文件夹中会复杂得多。
See the following answser on another thread: https://stackoverflow.com/a/10528920/848419
请参阅另一个线程上的以下答案:https://stackoverflow.com/a/10528920/848419