Serilog Eventid is logged as Json

125 views Asked by At

logger:

    ILogger logger = new LoggerConfiguration()
                    .ReadFrom.Configuration(configuration, "Logging")
                    .Enrich.FromLogContext()
                    .CreateLogger();

settings:

  "Logging": {
    "Using": [ "Serilog.Exceptions", "Serilog", "Serilog.Sinks.File"],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "System": "Information",
        "Microsoft": "Information",
        "Microsoft.EntityFrameworkCore": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "./logs/events.log",
          "rollingInterval": "Day",
          "retainedFileCountLimit": 7,
          "buffered": true,
          "outputTemplate": "{Timestamp:u};{host_ip};{enviroment_hostname};{EventId};{EventCategory};{EventSeverity};{Message:lj};{UserId};{ResourceType};{ResourceId}",
          "restrictedToMinimumLevel": "Information"
        }
      }
    ],
    "Enrich": [ "WithExceptionDetails", "WithProcId", "WithFacility", "ProcessHostEnricher", "WithSeverity" ]
  }

In the file I get the following output:

2023-01-13 15:43:25Z;;;{ Id: 101 };UserRolesUnAssigned;UserManagement;Medium;User:system roles un-assigned:role1,role2,role3;system;;

{EventId} comes out as a json object. I would need just the Id. so 101 instead of { Id: 101 }

EDIT: LoggerMessageDefinition:

private static Action<ILogger, string, string, Exception?> UserRolesUnAssigned =>
    LoggerMessage.Define<string, string>(LogLevel.Information, 101, UserMessageCodes[101].Template);
public static void LogUserRolesUnAssigned(this ILogger logger, string userId, string roleNames)
{
    EventDefinition eventDefinition = UserMessageCodes[101];
    using (LogContext.PushProperty(EventConstants.EventName, eventDefinition.Name))
    using (LogContext.PushProperty(EventConstants.EventCategory, eventDefinition.Category))
    using (LogContext.PushProperty(EventConstants.EventSeverity, eventDefinition.Severity))
        UserRolesUnAssigned(logger,userId, roleNames, null);
}

Is there a way to do this out of the box or do I need to create a custom enricher for this?

0

There are 0 answers