Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

mono - Serilog doesn't log to file, only to console

I'm currently working on a mono project and want to use Microsoft.Logging + Serilog there, but I stumble across the following problem: When I configure Serilog by hand, it logs in both console and the file (if I have configured both parameters). But when I use a configuration file, Serilog always ignores logging to the file.

The configuration file is found and the parameters are read from there, because if I add "Console" there, Serilog logs into the console.

With these settings, Serilog should, as I understand it, log into the console as well as into the file. I'm expecting this log file in "folder with appsettings.json"/logs. But it only logs into the console. And when I delete "Console", it doesn't log anywhere. I don't understand why this is so I am posting a few sections of code that are fundamental. If I have not provided enough information, please write to me in comments under the question.

appsettings.json:

{
    "Serilog": {
        "Using": [
            "Serilog.Sinks.Console"
        ],
        "MinimumLevel": "Debug",
        "Enrich": [
            "FromLogContext",
            "WithMachineName",
            "WithProcessId",
            "WithThreadId"
        ],
        "WriteTo": [
            {
                "Name": "File",
                "Args": {
                    "Path": "logs/GUIDemo.log",
                    "FileSizeLimitBytes": "400000",
                    "RollingInterval": "Day",
                    "RetainedFileCountLimit": "15",
                    "OutputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Level:u3}] {Message:lj} {NewLine}{Exception}",
                    "shared": true
                }
            },
            "Console"
        ]
    }
}

Here is how I configure Serilog in one class, which methods are executed shortly after the application start:

    private IContainer container;
    private ILogger<Bootstrapper> logger;
    ...
    public void OnStartup()
    {
        this.Configuration = this.ConfigureConfiguration();
        var builder = new ContainerBuilder();
        builder.RegisterModule<AutofacModule>();
        this.container = builder.Build();
        this.AddLogger();
        this.logger = this.container.Resolve<ILogger<Bootstrapper>>();
        this.LogApplicationStart();
    }

And finally methods AddLogger(), LogApplicationStart() and LogApplicationStart():

private void AddLogger()
{
    var loggerFactory = this.container.Resolve<ILoggerFactory>();
    loggerFactory.AddSerilog();

    Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(this.Configuration)
                .CreateLogger();
}

private IConfigurationRoot ConfigureConfiguration()
{
    return new ConfigurationBuilder().SetBasePath(System.IO.Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", false, true)
            .Build();
}

private void LogApplicationStart()
{
    this.logger.LogInformation("App is started");
}

After all of this, the output in the console is (yes, it is Godot):

Mono: Log file is: 'C:UsersAlbertAppDataRoaming/Godot/mono/mono_logs/2021_01_27 12.46.37(105008).txt'

[12:46:38 INF] App is started

The Mono log file has nothing to do with my log file. I also tried to add Log.CloseAndFlush() at the the end of LogApplicationStart() which is currently the only method in the app, but it didn't help me as well.

Of course, I know there are many similar question here, but I could not find a solution for my problem.

question from:https://stackoverflow.com/questions/65918636/serilog-doesnt-log-to-file-only-to-console

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...