serilog appSettings 写入同一服务器路径中的文件 asp.net mvc

serilog appSettings write to file in the same server path asp.net mvc

我有一个 serilog 代码,我想将配置移动到 web.config 文件。

一切正常,但问题出在文件路径上:

var logFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"EnBW.DokumentErfassung.Web-{Environment.MachineName}.log");
    Log.Logger = new LoggerConfiguration()
                        .ReadFrom.AppSettings();

我想在同一个服务器路径下写入新文件, web.config 代码是:

<add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" />
    <add key="serilog:write-to:RollingFile.pathFormat" value="log123-{Date}.txt" />

但是保存的文件已经保存在C:/program file/.....

我希望它保存在相同的域路径中。我应该怎么办?

app settings provider支持环境变量,所以在Global.asax.cs配置logger之前:

Environment.SetEnvironmentVariable("BASEDIR", AppDomain.CurrentDomain.BaseDirectory);

并在配置中使用%BASEDIR%

<add key="serilog:write-to:RollingFile.pathFormat"
     value="%BASEDIR%\logs\log-{Date}.txt" />