How to create Windows EventLog source from command line?

后端 未结 8 1473
温柔的废话
温柔的废话 2020-11-27 09:28

I\'m creating an ASP.NET application that will log some stuff to Windows EventLog. To do this an event source has to be created first. This requires administrative priviledg

相关标签:
8条回答
  • 2020-11-27 09:47

    You can also use Windows PowerShell with the following command:

    if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false) {
        [System.Diagnostics.EventLog]::CreateEventSource($source, "Application")
    }
    

    Make sure to check that the source does not exist before calling CreateEventSource, otherwise it will throw an exception.

    For more info:

    • http://msdn.microsoft.com/en-us/library/9t766zhb.aspx
    0 讨论(0)
  • 2020-11-27 09:55

    eventcreate2 allows you to create custom logs, where eventcreate does not.

    0 讨论(0)
  • 2020-11-27 09:58

    you can create your own custom event by using diagnostics.Event log class. Open a windows application and on a button click do the following code.

    System.Diagnostics.EventLog.CreateEventSource("ApplicationName", "MyNewLog");
    

    "MyNewLog" means the name you want to give to your log in event viewer.

    for more information check this link [ http://msdn.microsoft.com/en-in/library/49dwckkz%28v=vs.90%29.aspx]

    0 讨论(0)
  • 2020-11-27 10:02

    Try "eventcreate.exe"

    An example:

    eventcreate /ID 1 /L APPLICATION /T INFORMATION  /SO MYEVENTSOURCE /D "My first log"
    

    This will create a new event source named MYEVENTSOURCE under APPLICATION event log as INFORMATION event type.

    I think this utility is included only from XP onwards.

    Further reading

    • Windows IT Pro: JSI Tip 5487. Windows XP includes the EventCreate utility for creating custom events.

    • Type eventcreate /? in CMD prompt

    • Microsoft TechNet: Windows Command-Line Reference: Eventcreate

    • SS64: Windows Command-Line Reference: Eventcreate

    0 讨论(0)
  • 2020-11-27 10:02

    However the cmd/batch version works you can run into an issue when you want to define an eventID which is higher then 1000. For event creation with an eventID of 1000+ i'll use powershell like this:

    $evt=new-object System.Diagnostics.Eventlog(“Define Logbook”)
    $evt.Source=”Define Source”
    $evtNumber=Define Eventnumber
    $evtDescription=”Define description”
    $infoevent=[System.Diagnostics.EventLogEntryType]::Define error level
    $evt.WriteEntry($evtDescription,$infoevent,$evtNumber) 
    

    Sample:

    $evt=new-object System.Diagnostics.Eventlog(“System”)
    $evt.Source=”Tcpip”
    $evtNumber=4227
    $evtDescription=”This is a Test Event”
    $infoevent=[System.Diagnostics.EventLogEntryType]::Warning
    $evt.WriteEntry($evtDescription,$infoevent,$evtNumber)
    
    0 讨论(0)
  • 2020-11-27 10:03

    If someone is interested, it is also possible to create an event source manually by adding some registry values.

    Save the following lines as a .reg file, then import it to registry by double clicking it:

    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\Application\YOUR_EVENT_SOURCE_NAME_GOES_HERE]
    "EventMessageFile"="C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\EventLogMessages.dll"
    "TypesSupported"=dword:00000007
    

    This creates an event source named YOUR_EVENT_SOURCE_NAME_GOES_HERE.

    0 讨论(0)
提交回复
热议问题