首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ClientIp内核中的Serilog在日志中显示.net?

如何使用ClientIp内核中的Serilog在日志中显示.net?
EN

Stack Overflow用户
提问于 2022-07-11 14:59:44
回答 2查看 852关注 0票数 2

项目正在使用.Net Core3.1 WebAPI。

我正在尝试将客户端设备的IP地址记录到控制台。我已经安装了这个包Serilog.Enrichers.ClientInfo v1.2.0,并遵循了它们的步骤。除了控制台之外,还有一个带有列ErrorLog的表ClientIp,我想在其中记录IP地址。正如您在输出中看到的那样,ClientIp和ClientAgent部分都是空的。我是不是遗漏了什么?另外,请验证语法是否正确。

这是我来自appsettings.json的Serilog配置

代码语言:javascript
复制
"Serilog": {
"using": [],
"MinimumLevel": {
  "Default": "Debug",
  "Override": {
    "Microsoft": "Warning",
    "System": "Warning",
    "Microsoft.Hosting.Lifetime": "Information"
  }
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithClientIp", "WithClientAgent" ],
"WriteTo": [
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {
        "WriteTo": [
          {
            "Name": "Console",
            "Args": {
              "outputTemplate": "[{Level:u3} {Timestamp:o}] {ClientIp} {ClientAgent} ({SourceContext}) {Message} {NewLine}"
            }
          }
        ]
      }
    }
  },
  {
    "Name": "Logger",
    "Args": {
      "configureLogger": {
        "Filter": [
          {
            "Name": "ByIncludingOnly",
            "Args": {
              "expression": "@l in ['Error', 'Fatal']"
            }
          }
        ],
        "WriteTo": [
          {
            "Name": "MSSqlServer",
            "Args": {
              "connectionString": "TestDbContext",
              "tableName": "ErrorLogs",
              "batchPostingLimit": 50,
              "autoCreateSqlTable": true,
               "columnOptionsSection": {
                "removeStandardColumns": [ "MessageTemplate" ],
                "customColumns": [
                  {
                    "ColumnName": "ClientIp",
                    "DataType": "VARCHAR",
                    "AllowNull": true,
                    "DataLength": 50
                    //"NonClusteredIndex": true
                  }
                ]
              }
            }
          }
        ]
      }
    }
  }
]

}

输出:

代码语言:javascript
复制
[INF 2022-07-11T20:13:49.0041535+05:30]   () APP:API service has 
started
[INF 2022-07-11T20:13:49.5737570+05:30]   
(Microsoft.Hosting.Lifetime) Now listening on: 
"https://localhost:7017"
[INF 2022-07-11T20:13:49.5987755+05:30]   
(Microsoft.Hosting.Lifetime) Now listening on: 
"http://localhost:5017"
[INF 2022-07-11T20:13:49.6185554+05:30]   
(Microsoft.Hosting.Lifetime) Application started. Press Ctrl+C to 
shut down.
[INF 2022-07-11T20:13:49.6255005+05:30]   
(Microsoft.Hosting.Lifetime) Hosting environment: "Development"
[INF 2022-07-11T20:13:49.6305270+05:30]   
(Microsoft.Hosting.Lifetime) Content root path: "****"
[ERR 2022-07-11T20:14:29.2967601+05:30]   () Testing exception
[ERR 2022-07-11T20:14:29.3735005+05:30]   
(Serilog.AspNetCore.RequestLoggingMiddleware) HTTP "GET" 
"/api/Hospitals" responded 500 in 970.4728 ms
EN

回答 2

Stack Overflow用户

发布于 2022-07-12 15:49:47

试试这个包天鹅绒核

代码语言:javascript
复制
app.UseSerilogRequestLogging(options =>
{
    // Customize the message template
    options.MessageTemplate = "{RemoteIpAddress} {RequestScheme} {RequestHost} {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms";

    // Emit debug-level events instead of the defaults
    options.GetLevel = (httpContext, elapsed, ex) => LogEventLevel.Debug;

    // Attach additional properties to the request completion event
    options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
    {
        diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
        diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
        diagnosticContext.Set("RemoteIpAddress", httpContext.Connection.RemoteIpAddress);
    };
});

查一下这个链接,也许会有帮助。

添加IP日志#38

票数 3
EN

Stack Overflow用户

发布于 2022-07-11 20:11:58

看起来您在using语句中缺少了"Serilog.Enrichers.ClientInfo" (appsettings.json ln 2)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72940591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档