Samy Sammour Samy Sammour - 15 days ago 12
ASP.NET (C#) Question

writing inside a specific column Serilog ASP.NET MVC C#

I am working on Serilog in ASP.NET MVC with C#.

I have to types of log, one as for admin level and one is for user level.

I want to make an Extra column called "Role" contains 1 for admin or 2 for user according to microsoft member ship.

I used this code:

string connectionString = ConfigurationManager.........;

Log.Logger = new LoggerConfiguration()
.WriteTo.MSSqlServer(connectionString, "Logs")

Log.Information("Web4 starting");
Log.Information("log location: {0}", AppDomain.CurrentDomain.BaseDirectory);

I already added the column into the database

how can I write into column after create it?


ColumnOptions allows you to tell the SQL Server sink about the column:

var columnOptions = new ColumnOptions
  AdditionalDataColumns = new Collection<DataColumn>
    new DataColumn {DataType = typeof (string), ColumnName = "Role"}

Passed to the logger configuration method:

.WriteTo.MSSqlServer(connectionString, "Logs", columnOptions: columnOptions)

The data needs to be attached to each log event:

class RoleEnricher : ILogEventEnricher
  public void Enrich(LogEvent logEvent, ILogEventPropertyFactory pf)
    var role = // Get the role from your identity provider
    logEvent.AddOrUpdateProperty(pf.CreateProperty("Role", role));

Then configure the logger to use the enricher: