Programmatically create a custom Eventlog view

北城以北 提交于 2019-12-23 21:42:09

问题


I want to programmatically create a custom Eventlog view in a C# application.

This is how to create a custom Eventlog view with the help of the Eventlog application from Microsoft Windows:

I searched the class System.Diagnostics.EventLog for a method that does the same as the button found in the Eventlog Application from Microsoft. Sadly, i couldn't find any functionality that covers my needs.

Has anyone ever programmatically created a custom Eventlog view in C# or knows a way that works?


回答1:


If just tested the approach found here:

try
{
    XmlTextWriter view = new XmlTextWriter("C:\\ProgramData\\Microsoft\\Event Viewer\\Views\\View_1.xml", Encoding.Unicode);
    // Root.
    view.WriteStartDocument();
    view.WriteStartElement("ViewerConfig");
    //Create Node for queryConfig
    view.WriteStartElement("QueryConfig");
    view.WriteStartElement("QueryParams");
    view.WriteStartElement("UserQuery");
    view.WriteEndElement();
    view.WriteEndElement();
    //QueryNode
    view.WriteStartElement("QueryNode");
    //....

    view.Close();
}
catch (XmlException ex)
{
    Console.WriteLine(ex.StackTrace);
}

This created a custom view for me.

Basically, custom views are xml files stored under C:\\ProgramData\\Microsoft\\Event Viewer\\Views\\ and you can just roll you own, by creating an xml document.

If you want to know how to format such an xml document, you can always fall back to the already pre defined querys in the Server Roles folder under C:\ProgramData\Microsoft\Event Viewer\Views\ServerRoles




回答2:


Possible solution is:

  1. Create a desired custom view using eventvwr.msc interface (shown on image provided by you).
  2. Export it to a .xml file and study/make little research about it's structure
  3. Write a code to generate such .xml file based on your needs or use already crafted and exported only replacing appropriate "placeholders" (event codes, event sources, etc.)
  4. Run eventvwr.exe with /v option like eventvwr.exe /v:MyView.xml (more options using eventvwr.exe /?)


来源:https://stackoverflow.com/questions/29695358/programmatically-create-a-custom-eventlog-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!