Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog is logging type rather than object content

I'm new to Serilog - trying it out to see if it will help. I'm using Serilog v2 and Serilog.Sinks.MsSqlServer v5

I have the following console app code:

static void Main(string[] args)
    {
        var logger = CreateLogger();

        var employee = new Person()
        {
            Name = "Rob",
            Age = 45
        };

        logger.Debug("Employee details {Employee}", employee);

        Console.ReadKey();

    }

private static ILogger CreateLogger()
    {

        string levelString = SSOSettingsFileManager.SSOSettingsFileReader.ReadString(
                            "LCC.Common", "Serilog.MinimumLevel");

        SerilogLevel level = (SerilogLevel)Enum.Parse(typeof(SerilogLevel), levelString);

        string conString = SSOSettingsFileManager.SSOSettingsFileReader.ReadString(
                            "LCC.Common", "Serilog.ConnectionString");

        var levelSwitch = new LoggingLevelSwitch();
        levelSwitch.MinimumLevel = (Serilog.Events.LogEventLevel)level;

        return new LoggerConfiguration()
            .MinimumLevel.ControlledBy(levelSwitch)
            .WriteTo.MSSqlServer(connectionString: conString, tableName: "Logs", autoCreateSqlTable: true)
            .CreateLogger();
    }

I would have expected the details of Person to be logged i.e. Name Rob and Age 45. However, I find the following logged to the Properties column on my Sql Server Sink:

<properties><property key='Employee'>ConsoleApplication1.Person</property></properties>

Where did I go wrong?

like image 316
Rob Bowman Avatar asked Dec 19 '22 03:12

Rob Bowman


1 Answers

here is Serilog documentation

For this task, Serilog provides the @ destructuring operator.

var sensorInput = new { Latitude = 25, Longitude = 134 };
Log.Information("Processing {@SensorInput}", sensorInput);

as you can see to do destructuring you have to set @ before key name. This is missed in your sample code

logger.Debug("Employee details {Employee}", employee);
like image 126
oleksa Avatar answered Jan 06 '23 09:01

oleksa