Skip to content

Shared file does not log properly #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stilnat opened this issue Feb 19, 2023 · 2 comments
Closed

Shared file does not log properly #277

stilnat opened this issue Feb 19, 2023 · 2 comments
Labels

Comments

@stilnat
Copy link

stilnat commented Feb 19, 2023

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
Copy link
Member

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
Copy link
Author

stilnat commented Feb 24, 2023

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants