Configuring SQL Agent Jobs to Write to Windows Event Log

Problem

SQL agent jobs are not created with notifications enabled by default. When creating a new job from within SQL Server Management Studio, you will see the following screen when navigating to the Notifications page.

default new job

You have several options on the Notifications page. You could send an email, a page, a net send, write to the application event log, or you could delete the job (which is hardly what I would call a ‘notification’).

A best practice for job management would be to write information to the application event log in the event of a job failure. Some monitoring tools and software, such as Operations Manager, relies on information being written to the event logs. If no information is being logged than it is possible you could be missing some valuable alerts.

In this tip I will go over how to set this up and also a simple way to turn this on for all of your jobs.

Solution

When creating SQL Agent jobs, make certain to configure the job to write to the application event log when the job fails.


Run this to find details on the event notifications for the jobs currently defined on your instance:





 SELECT [name], [notify_level_eventlog]
FROM msdb..sysjobs

A sample result set is as follows:

sysjobs results

The sample result set lists the four possible choices for the [notify_level_eventlog] column:










Column Name


Description

notify_level_eventlogThe possible notifications include the following:
    0 – No logging

    1 – Log on success

    2 – Log on failure

    3 – Log on either result

Failures are written to the event log with an eventID of 208. This is important for something like Operations Manager, which has a rule inside the SQL Management Pack that relies on job failures being written to the application event log.

opsmgr failed job config

Operations Manager also has a monitor for the last run status of a SQL Agent job.

opsmgr db view sql agent

This monitor does not raise an alert, however. It merely changes the health status of the SQL Agent job target. So you would need to be in the habit of going to your database state views in order to monitor for any failed jobs. Most people, myself included, would prefer to have the failures logged in the application event log and also to generate some email notifications as a reminder.

You can run the following query to find all the jobs defined on your instance that are not doing any logging.





 SELECT [name]
FROM msdb..sysjobs
WHERE [notify_level_eventlog] = 0

If you prefer and are using SQL Server 2008, you could run that same query against all the instances registered in your Central Management Server.

use CMS

From there, you can update your jobs to write to the application event log on a job failure with one statement.  I like to filter my update statement based upon the name of the job. If you prefer, you can remove the WHERE clause and simply update all of your jobs at the same time.





 UPDATE msdb..sysjobs
SET [notify_level_eventlog] = 2
WHERE [name] like ‘This one%’

You will want to be careful if you choose to do so. I have seen some applications that create jobs with a schedule that runs the job every minute. You may not want to write that many failures to your application event log, so be mindful of job schedules when enabling your job notifications.

 

Next Steps

Leave a Reply

Your email address will not be published. Required fields are marked *