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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With