[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

Force sql serve to write to eventlog

The list of all SQL Server errors:

SELECT * FROM sysmessages where msglangid = 1033

I want to force SQL Server to write all errors with a servery higher than X to the operating system event viewer because it's the only way I have to capture sql server errors with my office monitoring tool.

How to do it ?
2 Solutions
lcohanDatabase AnalystCommented:
You could set a  New SQL Alert under SQL Agent in SSMS that can do that for you.
migavAuthor Commented:
Help, I don't find how to write to event viwer ..
lcohanDatabase AnalystCommented:
My bad...I should have included more detail:

You can add a new alert as mentioned and under the response you can execute a job instead of sending notification and in that job you can use xp_logevent  stored proc to do what you need:

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Anthony PerkinsCommented:
Perhaps I am being dense but if you want to write to the Event Log, have you considered something like this:
RAISERROR ('Houston we have a problem!', 10, 1) WITH LOG
So, what if you want to log information messages, or messages of low severity? If you wish to have an alert on any errors that are of a severity less that 19, then you have to modify their entry in the sysmessages table to set them to be always logged. You do this using sp_alterMessage with the WITH_LOG option to set the dLevel column to 128 . If a message has been altered to be WITH_LOG, it is always subsequently written to the application log, however the error happens. Even if RAISERROR is executed without the WITH LOG option, the error that you have altered is written to the application log, and is therefore spotted by the alert. There are good reasons for wanting to do this, as it will then log, and optionally alert you to, syntax errors that are normally seen only by the end-user.

  You can force any error that is triggered programmatically to be written to the error log by using the WITH LOG parameter with the RAISERROR command. So, with a user-defined error severity (9) you can log an incident, cause an alert to be fired, which in turn emails someone, or runs a job, simply by using RAISERROR. Naturally, because the job that responds to the alert can be run by the Agent under a different User, you do not need to assign unsafe permissions to the ordinary user. You can use xp_LogEvent if, as is likely, you do not want the user to see the error. (Only the Raiserror call can utilize the 'PrintF' formatting  placeholders in the error messages, so logging the error with a formatted message using xp_logevent results in a literal recording of the message, string format identifiers and all.)

You can read your Forcibly   Logged errors by following parameters  

Example EXEC xp_ReadErrorLog 0, 1, NULL, NULL, @DatelessHour, @CurrentDate


migavAuthor Commented:
Thanks !

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now