ashilon
ashilon

Reputation: 1941

Serilog in ASP.NET Core - logging the EventId in its own column

ASP.NET Core logging has this concept of 'Log event ID',
so that we can log lines like this (from their documentation);

_logger.LogInformation(LoggingEvents.GET_ITEM, "Getting item {ID}", id);

Serilog writes this inforamtion, LoggingEvents.GET_ITEM, in the properties column

<property key='EventId'>
<structure type=''>
  <property key='Id'>1002</property>
</structure>

I've added a new column 'EventId' in the table in the database, and added this ColumnOptions to the logger

columnOptions.AdditionalDataColumns = new List<DataColumn>
        {
            new DataColumn {DataType = typeof(byte), ColumnName = "EventId"},
            new DataColumn {DataType = typeof(string), ColumnName = "SessionId"}
        };  

But Serilog doesn't write anything to the database. Only when I remove this line:
new DataColumn {DataType = typeof(byte), ColumnName = "EventId"},
does Serilog write the line to the databse, but without the EventId column.
How can I write the event id?

Upvotes: 1

Views: 2619

Answers (1)

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31822

EventId in ASP.NET Core isn't an integer, but rather a structure that may contain an integer, a string, (effectively) neither, or both.

https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Extensions.Logging.Abstractions/EventId.cs

When it reaches the SQL Server sink, it can't be converted into a numeric column, unfortunately. I think you'll need to specify the column as text for this to work.

Upvotes: 1

Related Questions