Search code examples
c#wpfenvironment-variablesserilogserilog-sinks-file

%DOCUMENTS% environment variable does not work in App.config file of a WPF application


I have a WPF application and want to use Serilog to log context in a local txt file. I have installed Serilog, Serilog.Sinks.File and Serilog.Settings.AppSettings packages.

After launching and terminating the tool, I fail to find the txt file in the folder. I guess I set the path value using wrong environment variable. Can someone helps me to correct?

In the App class in App.xaml.cs file, I add following code in the constructor App().

Log.Logger=new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger();
Log.Information("##########################################################");
Log.Information("############## Tool started...###############");
Log.Information("##########################################################");

In the App.config file I add following key-value pairs to set logger.

<appSettings>
  <add key="serilog:minimum-level" value="Verbose" />
  <add key="serilog:using:File" value="Serilog.Sinks.File" />
  <add key="serilog:write-to:File.path" value="%DOCUMENTS%\Logs\Tool-.txt" />
  <add key="serilog:write-to:File.rollingInterval" value="Day" />
  <add key="serilog:write-to:File.retainedFileCountLimit" value="10" />
</appSettings>

Update:

I changed the value to %USERPROFILE%\Documents\ESI_Effort_A350\Logs\ESIA350-.txt and it works now. Seems this to be a solution. But is there a direct environment varibale for Documents folder?


Solution

  • But is there a direct environment variable for the Documents folder?

    This should work in C#:

    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    

    In configuration, %USERPROFILE%\Documents should work just fine. %DOCUMENTS% is invalid.