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 118 forks source link

Shared file does not log properly #277

Closed stilnat closed 1 year ago

stilnat commented 1 year ago

For some context, I'm working on SS3D, an open source Unity video game, it's networked and I'm trying to use Serilog to log everything clients do. I have a shared file for that, using shared = true. Issue is, while the logging in the Unity console works as expected, the logs in file are truncated, like so :

13:53 [Information] [ID = HOST] ########## SERVER STARTING ! ########## 13:54 [Information] [ID = HOST] ########## SERVER STARTING ! ########## 13:54 [Information] [ID = HOST] ########## CLIENT 1 STARTING ! ########## 13:54 [Information] [ID = HOST] client made primary click 13:55 [Information] [ID = HOST] ########## CLIENT 2 STARTING ! ####13:55 [I13:56 [Information] [ID = HOST] client made primary click 13:56 [Information] [ID = HOST] client made primary click 13:56 [Information] [ID = HOST] client made primary click 13:56 [Information] [ID = HOST] client made primary click ck 13:55 [Information] [ID = 2] client made primary click

The result I was expecting was something like :

13:53 [Information] [ID = HOST] ########## SERVER STARTING ! ########## 13:54 [Information] [ID = HOST] ########## SERVER STARTING ! ########## 13:54 [Information] [ID = HOST] ########## CLIENT 1 STARTING ! ########## 13:54 [Information] [ID = HOST] client made primary click 13:55 [Information] [ID = HOST] ########## CLIENT 2 STARTING ! 13:55 [I13:56 [Information] [ID = HOST] client made primary click 13:55 [Information] [ID = 2] client made primary click 13:55 [Information] [ID = 2] client made primary click 13:55 [Information] [ID = 2] client made primary click 13:55 [Information] [ID = 1] client made primary click 13:55 [Information] [ID = 1] client made primary click 13:55 [Information] [ID = 1] client made primary click 13:56 [Information] [ID = HOST] client made primary click 13:56 [Information] [ID = HOST] client made primary click 13:56 [Information] [ID = HOST] client made primary click

As you can see, some lines just didn't show up in the file, and some were truncated. Code wise, I have a method that starts on clients and one on server, they both do nothing much more than setting the logger :

public override void OnStartClient()
{

    base.OnStartClient();

    if (IsHost)
    {
        return;
    }

    Log.Logger = new LoggerConfiguration()
        .Enrich.With(new ClientIdEnricher())
        .WriteTo.Unity3D()
        .WriteTo.File("C:/Users/Nat/Documents/GitHub/StilnatSS3DMain/Logs/LogSession.txt"
        , outputTemplate: "{Timestamp:HH:mm} [{Level}] [ID = {ClientId}] {Message}{NewLine}{Exception}"
        , shared: true)
        .CreateLogger();

    Log.Information("##########  CLIENT STARTING !  ##########");
}

public override void OnStartServer()
    {
        base.OnStartServer();
        if (IsServerOnly)
        {
            Log.Logger = new LoggerConfiguration()
                            .WriteTo.Unity3D()
                            .WriteTo.File("C:/Users/Nat/Documents/GitHub/StilnatSS3DMain/Logs/LogSession.txt"
                            , outputTemplate: "{Timestamp:HH:mm} [{Level}] [ID = SERVER] {Message}{NewLine}{Exception}"
                            , shared: true)
                            .CreateLogger();
        }
        else
        {
            Log.Logger = new LoggerConfiguration()
            .WriteTo.Unity3D()
            .WriteTo.File("C:/Users/Nat/Documents/GitHub/StilnatSS3DMain/Logs/LogSession.txt"
            , outputTemplate: "{Timestamp:HH:mm} [{Level}] [ID = HOST] {Message}{NewLine}{Exception}"
            , shared: true)
            .CreateLogger();
        }

        Log.Information("##########  SERVER STARTING !  ##########");
        ServerManager.OnRemoteConnectionState += HandleRemoteConnectionState;
}

Without shared equals true, and with an individual file for each client, this works as expected. Any idea how to solve the issue ?

bartelink commented 1 year ago

If you need a quick response, I'd suggest asking this on stack overflow instead; you have all the key ingredients for a good question there - here you're banking on finding someone who is both a Serilog maintainer/contributor (that watches the repo) who also understands the nature of the Unity3D integration, which is a very small one.

stilnat commented 1 year ago

@bartelink Thank you for your answer. True, but my hope was that my issue was not specific to Unity integration, more generally it's a concurrent access issue and I believed other application would have the same issue. I'm going to follow your advice anyway, I might get more luck.