---
title: How to Monitor Failed LogEvents in SQL Server with Nodinite
description: Learn how to monitor failed LogEvents in SQL Server using the Nodinite Database Monitoring Agent. Includes T-SQL stored procedures, configuration steps, threshold settings, and testing recommendations.
---

# Monitoring Failed LogEvents

<aside aria-label="Tip" class="nodinite-note nodinite-note--tip">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-lightbulb"></i> Tip</p>
<div class="nodinite-note__content"><p>Detect failed LogEvents early to prevent processing backlogs, data loss, and compliance gaps.</p></div>
</aside>

## How do I monitor failed LogEvents?

You can use the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Database Monitoring Agent][] to monitor the **LogEvents** table for failed entries. The Database Monitoring Agent provides automated, continuous monitoring with instant alerts through <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Alarm Plugins][], eliminating the need for manual SQL Server Agent job configuration.

## Why should I use the Database Monitoring Agent?

✅ **Automated monitoring** - Continuously evaluates your LogEvents table without manual SQL Agent setup  
✅ **Instant alerts** - Trigger notifications via email, Slack, Teams, or custom webhooks when thresholds are exceeded  
✅ **Centralized visibility** - View LogEvents monitoring status alongside all other integration resources in <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Monitor Views][]  
✅ **Self-service management** - Empower teams to execute troubleshooting scripts on-demand via [Remote Actions][]

## How do I create a monitoring stored procedure?

The stored procedure below follows the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Database Monitoring Agent][] conventions for [SQL Statements][] monitoring. Deploy this procedure to your SQL Server database, then configure it in <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> as described in the [SQL Statements documentation][SQL Statements].

### Key concepts

* **Return codes**: `-10` = ERROR, `10` = WARNING, `0` = OK
* **SELECT output**: The first column of the first row becomes the **Log Text** displayed in <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> resources
* **Thresholds**: Adjust `@errorLimit` and `@warningLimit` to match your operational tolerances
* **Lookback window**: Set `@lookbackHours` to `0` for all-time counts, or limit to recent failures (e.g., 24 hours)

Run this query on a schedule via the Database Monitoring Agent, or deploy it as a stored procedure in SQL Server Agent. Tune thresholds and the lookback window to match your operational needs.

```sql
-- Monitor failed LogEvents and return status codes:
--   -10 = ERROR,  10 = WARNING,  0 = OK
CREATE PROCEDURE dbo.MonitorFailedLogEvents
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE 
        @failedCount INT,
        @errorLimit INT = 50,        -- Adjust to your environment
        @warningLimit INT = 10,      -- Adjust to your environment
        @lookbackHours INT = 24,     -- 0 = count all; otherwise limit to recent window
        @ret INT = 0,
        @log NVARCHAR(400);

    SELECT @failedCount = COUNT(1)
    FROM dbo.LogEvents
    WHERE ValidationFailed = 1
      AND (
            @lookbackHours = 0
            OR Created >= DATEADD(hour, -@lookbackHours, SYSUTCDATETIME())
          );

    IF (@failedCount > @errorLimit)
    BEGIN
        SET @log = N'ERROR: ' + CAST(@failedCount AS NVARCHAR(12))
                 + N' failed LogEvents in last ' + CAST(@lookbackHours AS NVARCHAR(6)) + N'h (threshold ' 
                 + CAST(@errorLimit AS NVARCHAR(12)) + N')';
        SELECT @log;
        SET @ret = -10;
    END
    ELSE IF (@failedCount > @warningLimit)
    BEGIN
        SET @log = N'WARNING: ' + CAST(@failedCount AS NVARCHAR(12))
                 + N' failed LogEvents in last ' + CAST(@lookbackHours AS NVARCHAR(6)) + N'h (threshold '
                 + CAST(@warningLimit AS NVARCHAR(12)) + N')';
        SELECT @log;
        SET @ret = 10;
    END
    ELSE
    BEGIN
        SELECT N'OK';
    END

    RETURN @ret;
END;
GO
```

## How do I configure monitoring in Nodinite?

Once the stored procedure is deployed, configure it in <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> to enable automated monitoring:

### <i class="fal fa-forward-step" aria-hidden="true"></i> Step 1: Open Database Monitoring Agent configuration

Navigate to **Administration** → **Monitor Management** → [Monitor Agents][] → Select your [Database Monitoring Agent][]

### <i class="fal fa-forward-step" aria-hidden="true"></i> Step 2: Add SQL Statement

* Go to the **SQL Databases** tab and select your connection
* Click the **SQL Statements** tab
* Click <i class="fal fa-square-plus" aria-hidden="true"></i> **Add** to create a new SQL Statement configuration

### <i class="fal fa-forward-step" aria-hidden="true"></i> Step 3: Configure General settings

* <i class="fal fa-toggle-on" aria-hidden="true"></i> **Enabled**: ✅ Checked
* <i class="fal fa-tag" aria-hidden="true"></i> **Display Name**: `LogEvents - Failed Validation Monitor`
* <i class="fal fa-align-left" aria-hidden="true"></i> **Description**: `Monitors the LogEvents table for validation failures and triggers alerts when thresholds are exceeded`

### <i class="fal fa-forward-step" aria-hidden="true"></i> Step 4: Configure Monitoring Script

* <i class="fal fa-terminal" aria-hidden="true"></i> **Command Type**: `Stored Procedure`
* <i class="fal fa-code" aria-hidden="true"></i> **Execute Command**: `[dbo].[MonitorFailedLogEvents]`

### <i class="fal fa-forward-step" aria-hidden="true"></i> Step 5: Save and synchronize

* Click **Save and close**
* The agent will automatically pick up the new configuration on the next sync cycle
* The stored procedure will appear as a new [Resource][] in your [Monitor Views][]

### <i class="fal fa-forward-step" aria-hidden="true"></i> Step 6: Assign to Monitor View (optional)

Add the new resource to a Monitor View to enable alerting:

* Navigate to **Administration** → **Monitor Management** → **Monitor Views**
* Select or create a Monitor View
* Add the `LogEvents - Failed Validation Monitor` resource
* Configure [Alarm Plugins][] to receive notifications (email, Slack, Teams, webhooks)

## How do I test my monitoring configuration?

<aside aria-label="Tip" class="nodinite-note nodinite-note--tip">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-lightbulb"></i> Tip</p>
<div class="nodinite-note__content"><p>After setup, verify monitoring is working by intentionally inserting an invalid LogEvent into the table. The Database Monitoring Agent should detect the failure within the configured polling interval (typically 60-300 seconds) and trigger an alert if thresholds are exceeded.</p></div>
</aside>

### Test steps

1. Insert an invalid LogEvent (malformed JSON):

   ```sql
   INSERT INTO dbo.LogEvents (LogEvent, ValidationFailed) 
   VALUES (N'{invalid-json', 0);
   ```

2. Wait for the next monitoring cycle (60-300 seconds)

3. Check the resource status in your Monitor View

4. Verify that alerts are triggered if thresholds are exceeded

5. Clean up the test data:

   ```sql
   DELETE FROM dbo.LogEvents 
   WHERE LogEvent = N'{invalid-json';
   ```

## What do the return codes mean?

<aside aria-label="Note" class="nodinite-note nodinite-note--note">
<p class="nodinite-note__label" aria-hidden="true"><i class="far fa-fw fa-note"></i> Note</p>
<div class="nodinite-note__content"><p>The Database Monitoring Agent evaluates SQL Statements based on:</p><ul>
<li><strong>Return code</strong>: <code>-10</code> = Error state, <code>10</code> = Warning state, <code>0</code> = OK state</li>
<li><strong>SELECT output</strong>: First column of first row becomes the Log Text displayed in the resource</li>
</ul><p>See the complete <a href="https://docs.nodinite.com/logging-and-monitoring/database/features/sql-statements/sql-server-statements/">SQL Statements</a> documentation for advanced configuration options, including User Action Scripts for on-demand troubleshooting.</p></div>
</aside>

### Return code meanings

|Return Code|State|Description|  
|---|---|---|  
|`-10`|❌ Error|Failed LogEvents exceed error threshold (`@errorLimit`)|  
|`10`|⚠️ Warning|Failed LogEvents exceed warning threshold (`@warningLimit`)|  
|`0`|✅ OK|Failed LogEvents below warning threshold or none found|  

## How do I adjust monitoring thresholds?

Edit the stored procedure and modify these variables to match your environment:

```sql
@errorLimit INT = 50,        -- Trigger error state above this count
@warningLimit INT = 10,      -- Trigger warning state above this count
@lookbackHours INT = 24,     -- Time window for counting failures
```

**Recommendations:**

* **High-volume environments**: Increase thresholds (e.g., `@errorLimit = 500`, `@warningLimit = 100`)
* **Low-volume environments**: Decrease thresholds (e.g., `@errorLimit = 10`, `@warningLimit = 3`)
* **Real-time monitoring**: Use shorter lookback windows (e.g., `@lookbackHours = 1`)
* **Trend analysis**: Use longer lookback windows (e.g., `@lookbackHours = 168` for 7 days)

## <i class="fal fa-hand-point-right" aria-hidden="true"></i> Next Steps

* <i class="fal fa-broom" aria-hidden="true"></i> [How to Safely Purge Failed LogEvents from SQL Server][FAQ-Maintenance]  

## <i class="fal fa-link-horizontal" aria-hidden="true"></i> Related Topics

* <i class="fal fa-cog" aria-hidden="true"></i> [SQL Server Configuration][SQLServer]  
* <i class="fal fa-balance-scale" aria-hidden="true"></i> [Pros and Cons][FAQ-Pros-And-Cons]  
* <i class="fal fa-broom" aria-hidden="true"></i> [Maintenance and Purging][FAQ-Maintenance]  
* <i class="fal fa-vial" aria-hidden="true"></i> [Testing and Test Data][FAQ-Testing]  

[Alarm Plugins]:https://docs.nodinite.com/administration/settings/alarm-plugins/features/what-is-an-alarm-plugin/
[Database Monitoring Agent]:https://docs.nodinite.com/logging-and-monitoring/database/overview/
[FAQ-Maintenance]:https://docs.nodinite.com/logging-and-monitoring/pickup-logevents-service/features/configurations/troubleshooting/faq-sql-server-maintenance/
[FAQ-Pros-And-Cons]:https://docs.nodinite.com/logging-and-monitoring/pickup-logevents-service/features/configurations/troubleshooting/faq-sql-server-pros-and-cons/
[FAQ-Testing]:https://docs.nodinite.com/logging-and-monitoring/pickup-logevents-service/features/configurations/troubleshooting/faq-sql-server-testing/
[Monitor Agents]:https://docs.nodinite.com/administration/monitor-management/monitor-agents/features/what-is-a-monitor-agent/
[Monitor Views]:https://docs.nodinite.com/administration/monitor-management/monitor-views/features/what-is-a-monitor-view/
[Remote Actions]:https://docs.nodinite.com/getting-started/monitor/remote-actions/
[Resource]:https://docs.nodinite.com/administration/monitor-management/resources/features/what-is-a-resource/
[SQL Statements]:https://docs.nodinite.com/logging-and-monitoring/database/features/sql-statements/sql-server-statements/
[SQLServer]:https://docs.nodinite.com/logging-and-monitoring/pickup-logevents-service/features/configurations/sqlserver/