小编for*_*ian的帖子

如何在 appsettings.json 中设置我自己的 KeyGenerator 实例?

我已经在这里问过这个问题但我觉得 Stackoverflow 可能会更快。这就是我尝试在我的 json 配置文件中执行此操作的方式:

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.AzureTableStorage" ],
    "WriteTo": [
      {
        "Name": "AzureTableStorage",
        "Args": {
          "storageTableName": "Logs",
          "connectionString": "*************",
          "keyGenerator": "MyApp.Serilog.AzureTableStorage.MyKeyGenerator"
        }
      }
    ],
    "MinimumLevel": "Verbose"
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的生成器实现:

public class MyKeyGenerator : IKeyGenerator
{
    public string GeneratePartitionKey(LogEvent logEvent)
    {
        return Environment.MachineName;
    }

    public string GenerateRowKey(LogEvent logEvent, string suffix = null)
    {
        return SUID.nextId().ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,该.ReadFrom.Configuration操作会抛出一个InvalidCastException,因为它试图将字符串内容放入一个IKeyGenerator参数中。

我应该如何设置keyGenerator参数以确保MyKeyGenerator创建类的实例并将其提供给该参数?

json azure-table-storage serilog asp.net-core-2.0

5
推荐指数
1
解决办法
640
查看次数