datalust / serilog-sinks-seq

A Serilog sink that writes events to the Seq structured log server
https://datalust.co/seq
Apache License 2.0
239 stars 50 forks source link

Ignoring a specific error. #82

Closed simonpbond closed 7 years ago

simonpbond commented 7 years ago

Hello,

I'm wondering if it is possible to hide a specific error from being logged.

I'm using Microsoft's preview release of SignalR core, which currently logs an error on every disconnect. This is a known bug in this preview release, and does not affect the actual functionality of SignalR.

2017-08-16T10:33:42.7178631+05:45: ConnectionId 3f7f3146-c1b6-437a-b6cc-a53d76508254: Failed disposing connection.

The error message I want to ignore will always contain "Failed disposing connection."

Is there anyway to ignore a specific message based on a string I provide?

Thanks in advance -Simon

bbrandt commented 7 years ago

Hi Simon, Thanks for posting your question. I'm excited about the upcoming SignalR release as well.

I think this could be a good use case for the Serilog.Filters.Expressions library. Nicholas Blumhardt wrote a great article here introducing the library.

What I'm not sure of, that I'll need your help with, is the property key names in the "Failed disposing connection" log entry you get from SignalR. So, I'm not sure if it'd be better to filter on the resulting message or on a property which is likely faster.

Once including Serilog.Filters.Expressions, this may get you part of the way there:

var expr = "@Level = 'Error' and Contains(@Message, 'Failed disposing connection')";

Log.Logger = new LoggerConfiguration()
    .Enrich.WithProperty("AppId", 10)
    .Filter.ByExcluding(expr)
    .WriteTo.LiterateConsole()
    .CreateLogger();
simonpbond commented 7 years ago

Thanks for taking the time to help me out, It looks like the SignalR team are very close to fixing up that error, but will give it a try out of interest & learning :)

Thanks again!