serilog / serilog-sinks-file

Write Serilog events to files in text and JSON formats, optionally rolling on time or size
Apache License 2.0
334 stars 117 forks source link

PublishSingleFile=true don't generate log files #272

Closed atliuhui closed 1 year ago

atliuhui commented 1 year ago

net7.0

dotnet new web

dotnet add package Serilog.AspNetCore --version 6.1.0

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "logs/.log",
          "rollingInterval": "Day",
          "rollOnFileSizeLimit": true,
          "fileSizeLimitBytes": 10485760
        }
      }
    ]
  }
}

Program.cs

using Serilog;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog((context, services, configuration) => configuration
    .ReadFrom.Configuration(context.Configuration)
    .Enrich.FromLogContext()
    .WriteTo.Console());

var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

FolderProfile.pubxml

<?xml version="1.0" encoding="utf-8"?>
<Project>
  <PropertyGroup>
    <DeleteExistingFiles>true</DeleteExistingFiles>
    <ExcludeApp_Data>false</ExcludeApp_Data>
    <LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>bin\Release\net7.0\publish\</PublishUrl>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <_TargetId>Folder</_TargetId>
    <SiteUrlToLaunchAfterPublish />
    <TargetFramework>net7.0</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>true</PublishSingleFile>
    <ProjectGuid>38063059-68b5-41ee-b324-71080dbe2454</ProjectGuid>
    <SelfContained>false</SelfContained>
  </PropertyGroup>
</Project>

run test.exe don't generate log files. but when PublishSingleFile=false, generate log files is OK.

SimonCropp commented 1 year ago

to save us some time, can u push up the full solution to a github repository

atliuhui commented 1 year ago

please pull this repo: https://github.com/atliuhui/debug-serilog

billhong-just commented 1 year ago

Me too 👀

daren-porter commented 1 year ago

This seems very similar to an issue that I ran into with LogContext not working correctly when PublishSingleFile=true: https://github.com/serilog/serilog/issues/1558

TL;DR - Automatic discovery of configuration assemblies is not supported in bundled mode. You may need to explicitly list the Serilog assembly names being referenced in the "Using" array of the Serilog settings section. Something like this:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Serilog": {
    "Using": [      <------ This section!
        "Serilog",
        "Serilog.Sinks.File",
        "Serilog.Settings.Configuration",
        ... etc
    ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "logs/.log",
          "rollingInterval": "Day",
          "rollOnFileSizeLimit": true,
          "fileSizeLimitBytes": 10485760
        }
      }
    ]
  }
}