Serilog破坏了多个日志? [英] Serilog clobbering over multiple logs?

查看:51
本文介绍了Serilog破坏了多个日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对于单词来说太离奇了.我有2个完全独立的程序,都使用Serilog,例如:

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 ();

第二程序记录到"testB-.log".现在,奇怪的是,当程序B登录到"testB-.log"时,它结束于"testA-.log"中.(哎呀!).没事这就像Windows所说的"Ony 1程序可以同时使用此dll".哈哈.这里发生了什么奇怪的合并?!

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?!

这些程序通过SHVDN运行: https://github.com/crosire/scripthookvdotnet/releases

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

推荐答案

似乎您正在使用 ScriptHookVDotNET 在存储记录器实例( Log.Logger )时共享相同的静态上下文,因此运行的最后一个脚本会更改实例并影响第一个脚本.

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 似乎并未提供您在常规.NET应用程序中期望的隔离,因此建议您在他们的聊天室中询问,这是设计使然,以及建议的"; ScriptHookVDotNET 方式";创建隔离的静态上下文的过程.

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");
            // ...
        }

        // ...
    }
}

这篇关于Serilog破坏了多个日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆