Serilog为什么不记录到文件?

huangapple go评论51阅读模式
英文:

Why serilog not logging to file?

问题

以下是翻译好的部分:

"WriteTo": [
  {
    "Name": "File"
  },
  {
    "Name": "File",
    "Args": {
      "configureLogger": {
        "WriteTo": [
          {
            "Name": "File",
            "Args": {
              "path": "Logs/Error/applog.log",
              "outputTemplate": "{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
              "rollingInterval": "Day",
              "retainedFileCountLimit": 7
            }
          }
        ]
      }
    }
  }
]

在你的应用程序的appsettings.json文件中有上述内容。

为什么Serilog没有将日志写入 "path": "Logs/Error/applog.log"?

var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

var Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .CreateLogger();

Logger.Information("Hello, world!");

你的设置文件如下:

{
  "Logging": {
    "Serilog": {
      "WriteTo": [
        {
          "Name": "File",
          "Args": {
            "path": "Logs/Error/applog.log",
            "outputTemplate": "{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
            "rollingInterval": "Day",
            "retainedFileCountLimit": 7
          }
        }
      ]
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "HrSoultion": "server=.;database=Maqta;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True"
  }
}
英文:
"WriteTo": [
      {
        "Name": "File"
      },
      {
        "Name": "File",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/Error/applog.log",
                  "outputTemplate": "{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
                  "rollingInterval": "Day",
                  "retainedFileCountLimit": 7
                }
              }
            ]
          }
        }
      }

I have this in my app setting.json file?

Why serilog not writting to "path": "Logs/Error/applog.log", ?

var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();

        var Logger = new LoggerConfiguration()
          .ReadFrom.Configuration(configuration)
          .CreateLogger();

        Logger.Information("Hello, world!");

I have this setting file ?

{
  "Logging": {
    "Serilog": {
      "WriteTo": [
        {
          "Name": "File",
          "Args": {
            "path": "Logs/Error/applog.log",
            "outputTemplate": "{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
            "rollingInterval": "Day",
            "retainedFileCountLimit": 7
          }
        }
      ]
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "HrSoultion": "server=.;database=Maqta;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True"
  }
}

答案1

得分: 2

Your configuration in appsettings.json doesn't seem to be correct.

If you provide this configuration, it should work:

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "Logs/Error/applog.log",
          "outputTemplate": "{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
          "rollingInterval": "Day",
          "retainedFileCountLimit": 7
        }
      }
    ]
  }
}

My result:

Serilog为什么不记录到文件?


For reference, I used .NET 6 and these NuGet packages:

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
    <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
  </ItemGroup>

Links:

英文:

Your configuration in appsettings.json doesn't seem to be correct.

If you provide this configuration, it should work:

{
  &quot;Serilog&quot;: {
    &quot;WriteTo&quot;: [
      {
        &quot;Name&quot;: &quot;File&quot;,
        &quot;Args&quot;: {
          &quot;path&quot;: &quot;Logs/Error/applog.log&quot;,
          &quot;outputTemplate&quot;: &quot;{Timestamp:o} [Thread:{ThreadId}] [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}&quot;,
          &quot;rollingInterval&quot;: &quot;Day&quot;,
          &quot;retainedFileCountLimit&quot;: 7
        }
      }
    ]
  }
}

My result:

Serilog为什么不记录到文件?


For reference, I used .NET 6 and these NuGet packages:

  &lt;ItemGroup&gt;
    &lt;PackageReference Include=&quot;Microsoft.Extensions.Configuration&quot; Version=&quot;6.0.0&quot; /&gt;
    &lt;PackageReference Include=&quot;Microsoft.Extensions.Configuration.Json&quot; Version=&quot;6.0.0&quot; /&gt;
    &lt;PackageReference Include=&quot;Serilog.Settings.Configuration&quot; Version=&quot;3.4.0&quot; /&gt;
    &lt;PackageReference Include=&quot;Serilog.Sinks.File&quot; Version=&quot;5.0.0&quot; /&gt;
  &lt;/ItemGroup&gt;

Links:

huangapple
  • 本文由 发表于 2023年2月16日 03:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定