问题
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
回答1:
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");
// ...
}
// ...
}
}
来源:https://stackoverflow.com/questions/65365717/serilog-clobbering-over-multiple-logs