How to get Serilog to log to the web output directory from appsettings.json?

后端 未结 1 704
既然无缘
既然无缘 2021-01-06 07:57

I created a new .NET Core web project using Visual Studio and integrated Serilog. It reads settings from the appsettings.json (via .UseSerilog((ctx, config) => { co

相关标签:
1条回答
  • 2021-01-06 08:51

    Ideally, you don't want to write log files to the content root of your web app, since these could end up being accessible over the web, which would be a serious security consideration.

    The log file path configuration can include environment variables, so something like %TMP%\log.txt, or a path based on %LOCALAPPDATA% etc. is a good option (as long as the web site's worker process has permission to write to the location).

    You can write to a path that's relative to your web app by changing the current directory before setting up the logger:

    Environment.CurrentDirectory = AppContext.BaseDirectory;
    

    Or, you can combine these approaches and set your own environment variable to do it:

    Environment.SetEnvironmentVariable("BASEDIR", AppContext.BaseDirectory);
    

    Thus the following config:

    "%BASEDIR%\logs\log.txt"
    
    0 讨论(0)
提交回复
热议问题