在特定列内写入Serilog ASP.NET MVC C#

时间:2016-10-10 10:45:05

标签: c# asp.net asp.net-mvc serilog

我正在使用C#在ASP.NET MVC中使用Serilog。

我需要记录类型,一个用于管理级别,一个用于用户级别。

我希望根据microsoft成员发送一个名为“Role”的Extra列,其中包含1个用于admin或2个用于用户。

我使用了这段代码:

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

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.MSSqlServer(connectionString, "Logs")
                .Enrich.WithExceptionDetails()
                .Enrich.With<HttpRequestIdEnricher>()
                .Enrich.FromLogContext()
                .CreateLogger();

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

我已将该列添加到数据库中

创建后如何写入列?

1 个答案:

答案 0 :(得分:2)

debug1: Connecting to ec2-[id].compute-1.amazonaws.com [id] port 22. debug1: Connection established. debug1: Requesting no-more-sessions@openssh.com debug1: Entering interactive session. debug2: callback start debug1: Sending command: scp -v -t /var/app/current/config debug2: channel 0: request exec confirm 1 debug2: callback done debug2: channel 0: open confirm rwindow 0 rmax 32768 debug2: channel 0: rcvd adjust 2097152 debug2: channel_input_status_confirm: type 99 id 0 debug2: exec request accepted on channel 0 Sending file modes: C0644 1706 pk-cfappkey.pem debug2: channel 0: rcvd ext data 45 Sink: C0644 1706 pk-cfappkey.pem debug2: channel 0: written 45 to efd 8 scp: /var/app/current/config/pk-cfappkey.pem: Permission denied debug2: channel 0: read<=0 rfd 6 len 0 debug2: channel 0: read failed debug2: channel 0: close_read debug2: channel 0: input open -> drain debug2: channel 0: ibuf empty debug2: channel 0: send eof debug2: channel 0: input drain -> closed 允许您告诉SQL Server接收器有关该列的信息:

ColumnOptions

传递给记录器配置方法:

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

需要将数据附加到每个日志事件:

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

然后配置记录器以使用richher:

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));
  }
}