How to create Windows EventLog source from command line?
Asked Answered
D

9

192

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 priviledges so I cannot do it in the ASP.NET app.

Is there an existing command-line application that is bundled with Windows that can create an event log source, or must I roll out my own?

Dubrovnik answered 15/1, 2009 at 13:22 Comment(0)
K
319

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

Kile answered 15/1, 2009 at 13:23 Comment(8)
you have to right click on "cmd" and choose "run as admin" from vista onGanesa
eventcreate records an event under an existing source, it will not create a new source from scratch as the OP requested.Lightweight
@PaulChavez if the named source doesn't exist, it is created.Unfledged
This worked well for me- I can confirm that it created a source that did not previously exist.Handclasp
This won't create the event if the MYEVENTSOURCE already exists and was created using something other than eventcreateDisposed
This worked well for me too. In my scenario, I have a .net windows service that doesn't have permission to create an event source. So running this command, fixed this by creating the source. Which means my code started logging to the event log, and I didn't have to expand the permissions for my windows service user context.Quadrangular
whilst this worked and created a new source all my events all had "The description for Event ID 0 from source myApp cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted" so I had to edit the registry in the endNichollenicholls
Great solution. Is there an alternative utility that would allow event IDs > 1000 ?Slaver
F
191

Try PowerShell 2.0's EventLog cmdlets. For PowerShell 2.0 and upwards:

  • Run New-EventLog once to register the event source:

      New-EventLog -LogName Application -Source MyApp
    
  • Then use Write-EventLog to write to the log:

      Write-EventLog 
          -LogName Application 
          -Source MyApp 
          -EntryType Error 
          -Message "Immunity to iocaine powder not detected, dying now" 
          -EventId 1
    
Fracture answered 22/8, 2011 at 23:29 Comment(4)
This works fine, just remember to run PowerShell with elevated privileges.Enactment
I had to open and close event viewer to see the new log that I createdCivilian
Also if you are actively developing and New-EventLog-ing and Remove-EventLog'-ing back and forth you might encounter a problem when Source is registered but does not write to specified Log. Restarting computer helps with that. Another tip: you can see what is going on with your event logs with regedit here: [Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\]Archaize
@amackay11, while closing-and-reopening Event Viewer does work, a quicker/easier way is to simply click on its Action menu and select Refresh.Satisfy
B
52

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:

Butene answered 19/2, 2010 at 22:53 Comment(0)
V
12

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

Vagary answered 5/10, 2009 at 21:12 Comment(0)
H
9

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.

Heehaw answered 26/11, 2015 at 12:14 Comment(0)
W
1

Or just use the command line command:

Eventcreate

Waftage answered 22/5, 2009 at 16:41 Comment(0)
M
1

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)
Minnick answered 10/9, 2015 at 14:9 Comment(0)
K
0

PowerShell 7

This answer worked great in 5.x for me but not in 7.x. After some sleuthing, I got the following working:

Import-Module Microsoft.PowerShell.Management -UseWindowsPowerShell
New-EventLog -LogName Application -Source MyApp

I stumbled upon the module to import via this SO answer. Apparently, there's a set of modules you can import for Windows only cmdlet's depending upon your needs. I'm still trying to figure out how you would determine which module to import based upon your cmdlet.

Kippar answered 13/10, 2022 at 20:13 Comment(0)
D
-3

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]

Dehorn answered 31/5, 2014 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.