How to Write Events to Windows Event Viewer log in SQL Server
MSSQL event viewer, SQL server, SQL Server event viewerBeing a part of database team, you will have some sort of monitoring system through which you would be receiving SQL Server alerts and these alerts are collected from SQL Server Error log and Event Viewer log. There are occasions wherein you are working on any monitoring setup and as part of that work, you want to generate some test alerts in event viewer log to see if monitoring tool is picking up that alert or not. In this article, we will see how to write events in the Windows Event Viewer log.
How to View Event Viewer Log:
search for word "event" from your windows search bar and you would see as below.
Method#1:
Method#2:
go to start button — >type eventvwr in search bar as shown below.
Windows event viewer log will get opened. now go to Windows Logs –> Application(as shown below). on right hand side of Application, you can see all the SQL Server related events along with some other Windows level events.
How to write events in Event Viewer Log:
Power Shell Syntax:
"Write-EventLog -LogName $LogName -Source $Source -EntryType $EntryType -EventId $EventID -Message $Message"
Power Shell Example:
Write-Eventlog -LogName Application -Source "MSSQLSERVER" -EntryType Error -EventID 24026 -Message "Monitoring Testing"
where,
Write-Eventlog –> this will write specified event to event viewer log.
LogName Application –> event will be written to application log as shown in above snap.
Source "MSSQLSERVER" –> since our events are related to SQL Server, so we will keep source as "MSSQLSERVER"
EntryType Error –> we will keep EntryType as Error
EventID 24026 –> mention the event id that you want to insert in event log.
Before inserting event in event log:
After inserting event in event log:
How to filter event id from event viewer:
First click on Filter Current Log from the Actions pane available on right hand side of event viewer log.
Mention event id that you want to filter from the log. Also, you can choose event level as per your requirement.
List of All event viewer ID's:
How useful was this post?
Click on a star to rate it!
Average rating 5 / 5. Vote count: 1
No votes so far! Be the first to rate this post.