.Net核心扩展SeriLog过滤不起作用

时间:2018-12-24 19:30:07

标签: asp.net-core-2.0 serilog

我正在使用.net Core 2.0.9和Serilog.Filters.Expressions 2.0.0。 我将appsettings.json配置为写入数据库的日志表中。数据已成功记录在数据库中,但RequestPath属性始终为null:

"Serilog": {
        "MinimumLevel": {
            "Default": "Debug",
            "Override": {
                "Microsoft": "Debug"
            }
        },
        "WriteTo": [
            {
                "Name": "MSSqlServer",
                "Args": {
                    "connectionString": "myconnectionString",
                    "tableName": "Log"
                }
            }
        ],
        "WriteTo:Async": {
            "Name": "Async",
            "Args": {
                "configure": [
                    {
                        "Name": "File",
                        "Args": {
                            "path": "..\\output\\log.txt",
                            "rollingInterval": "Day"
                        }
                    }
                ]
            }
        },
        "Using": [ "Serilog.Settings.Configuration" ]
        "Filter": [
            {
                "Name": "ByIncludingOnly",
                "Args": {
                    "expression": "RequestPath like '%/api/book%'"
                }
            }
        ]
    },

但是我只想过滤和保存具有特定api路径的日志条目。在这种情况下,仅在RequestPath中包含api / user路径的条目。但是没有数据被保存了,我也没有日志错误,为什么?

1 个答案:

答案 0 :(得分:0)

这是我的工作步骤,请检查差异:

  1. appsettings.json

    "Serilog": {
        "MinimumLevel": "Information",
        "Override": {
        "Microsoft": "Critical"
        },
        "WriteTo": [
        {
            "Name": "MSSqlServer",
            "Args": {
            "connectionString": "Data Source=xx",
            "autoCreateSqlTable ": true,
            "tableName": "Logs",
            "autoCreateSqlTable": true,
            "columnOptionsSection": {
                "removeStandardColumns": [ "Properties" ],
                "customColumns": [
                {
                    "ColumnName": "Release",
                    "DataType": "varchar",
                    "DataLength": 32
                },
                {
                    "ColumnName": "RequestPath",
                    "DataType": "varchar"
                },
                {
                    "ColumnName": "ConnectionId",
                    "DataType": "varchar"
                }
                ]
            }
            }
    
        },
        {
            "Name": "RollingFile",
            "Args": {
            "pathFormat": "Logs/app-{Date}.txt",
            "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message} {UserName} {ActionName}  {NewLine} {Exception}"
            }
        }
        ],
        "Using": [ "Serilog.Settings.Configuration" ],
        "Filter": [
        {
            "Name": "ByIncludingOnly",
            "Args": {
            "expression": "RequestPath like '%/api%'"
            }
        }
        ]
    },
    
  2. Startup.cs

        Log.Logger = new LoggerConfiguration()
                        .ReadFrom.ConfigurationSection(Configuration.GetSection("Serilog"))
                        .CreateLogger();
    

要检查serilog错误,请在下面添加代码:

    Log.Logger = new LoggerConfiguration()
                        .ReadFrom.ConfigurationSection(Configuration.GetSection("Serilog"))
                        .CreateLogger();
    Serilog.Debugging.SelfLog.Enable(msg =>
    {
        Debug.Print(msg);
        Debugger.Break();
    });