0

This is too bizarre for words. I have 2 completely separate programs, both of which use Serilog, like:

            Log.Logger = new LoggerConfiguration ()
            .MinimumLevel.Debug ()
            .Filter.ByExcluding (e => e.MessageTemplate.Text.Contains ("Could not find file"))
            .WriteTo.File ("testA-.log", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 1, outputTemplate: template)
            .CreateLogger ();

The 2nd program logs to "testB-.log". Now, the bizarre part is, that when program B logs to "testB-.log"... it winds up in "testA-.log" (Sic!). No shit. That's like Windows saying "Ony 1 program can use this dll at the time." LOL. What strange amalgamation is taking place here?!

N.B. These programs run via SHVDN: https://github.com/crosire/scripthookvdotnet/releases

  • No mistyping. :) Both programs run under SHVDN (ScriptHookVDotNET), for GTA V, which instantiates the programs. So, not entirely sure what goes on under the hood there. But both do "new LoggerConfiguration ()...CreateLogger ()". Shouldn't that guarantee separate instances? – meimeiriver Dec 19 '20 at 01:43
  • 1
    `CreateLogger` always creates a new instance - that's not the problem. The problem is where you're storing the new instance: `Log.Logger` - which seems to be shared between the two apps in your case. On a regular .NET host app, they wouldn't be shared, but on a custom host (`ScriptHookVDotNET`) it's up to the host to provide the isolation, and it looks like they don't. You should reach out to the `ScriptHookVDotNET` devs to understand why and see if there's a workaround – C. Augusto Proiete Dec 19 '20 at 01:56
  • Yeah, guess I'm screwed then. But thx for the explanation. – meimeiriver Dec 19 '20 at 02:10

1 Answers1

2

It seems that the two apps/scripts that you are executing with ScriptHookVDotNET are sharing the same static context where you're storing the logger instance (Log.Logger), so the last script that runs changes the instance and affects the first script.

ScriptHookVDotNET doesn't seem provide the isolation you'd expect in a regular .NET app, thus I'd suggest asking on their Gitter chat if this by design and what would be the recommended "ScriptHookVDotNET way" of creating an isolated static context.

One possible workaround is to store your logger instance as an instance variable that belongs to a specific script, which can be shared with the instance methods of the script. e.g.

public class Main : Script
{
    private ILogger _log = Logger.None;

    public Main()
    {
        _log = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .Filter.ByExcluding(e => e.MessageTemplate.Text.Contains("Could not find file"))
            .WriteTo.File("testA-.log", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 1)
            .CreateLogger();

        KeyDown += OnKeyDown;
        Interval = 1000;
    }

    private void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.T)
        {
            _log.Information("Create AI Script and store as AIone");
            // ...
        }

        // ...
    }
}
C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91