1

We are trying to switch completely from log4net to Serilog. However, this part of functionality seems to be missing. What I need is to be able to get location of log-files Inside a library class. This is important for our Desktop Click-Once application because that location is different on different OSes and for different users. When user needs access to the logs we can direct him to the proper folder. Very similar question was asked here: Read current Serilog's configuration But I can't believe that there is no way to get this information from Serilog. I don't need to change that configuration - just read it. In log4net we could do:

log4net.LogManager.GetAllRepositories()

and then

repository.Root.Appenders.OfType<FileAppender>

Please tell me that there is some kind back-door to the current LoggerConfiguration or if there is some alternative way to get file-path of the current File-Sink.

Greg Z.
  • 1,376
  • 14
  • 17

1 Answers1

2

Serilog does not expose the list of Sinks that have been configured, so your only option at the moment would be to use Reflection if you really want to get this information from the live Serilog configuration.

You can see an example on this answer:

Unit test Serilog configuration

That said, given all you want to do is to know the path where log files are being written, that's something you can easily store at the start of the application at the moment you set up your Serilog logging pipeline.

If you configure the file path in code, you can store that information in a static property somewhere your entire app can access. If you get your folder path from the appSettings.json or App.config, you can read the information from there.

If you have environment variables in your configuration you can get the same values that Serilog gets by expanding these environment variables e.g. Environment.ExpandEnvironmentVariables("%LogPath%\\AppName.log")

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • Augusto, thank you, but neither method (going through reflection or reading appsettings.json) works reliably. Reflection is annoying because I am getting sinks on different levels when going through hierarch, but more importantly, is not reliable because, I am getting RollingFileSink, for example, instead of expected FileSink and then I have to dig 2 levels dipper to get my LogFileDerctory. Reading straight from .config also doesn't work because my configuration has environment variables ("path": "%LogPath%\\%AssemblyName%.log"). That's the whole reason I want to get this configuration. Help! – Greg Z. Nov 02 '20 at 19:18
  • 1
    @GregZ. I believe Reflection would work reliably as long as you 1. Recursively unwrap the sinks you encounter, and 2. Know what you're looking for (`RollingFileSink` or `FileSink`). You should also be able to reliably get the same values that Serilog gets by expanding the environment variables e.g. `Environment.ExpandEnvironmentVariables("%LogPath%\\%AssemblyName%.log")` – C. Augusto Proiete Nov 02 '20 at 19:45
  • I ended up doing both, but boy is it a pain to go through reflection. I don't see how it could be reliable. For example, small fragment of my code for RollingFileSink: object roller=GetProperty(fileSink, "_roller"); string diectory=GetProperty(roller, "_directory"); string filenamePrefix = GetProperty(roller, "_filenamePrefix"); string filenameSuffix = GetProperty(roller, "_filenameSuffix"); return Path.Combine(diectory, filenamePrefix + filenameSuffix); They rename one of those fields and it falls apart. Can I ask for a feature? If yes, were? – Greg Z. Nov 04 '20 at 20:55
  • 1
    @GregZ. Got you. Yes, I see what you mean. Using reflection is only reliable within the same version... It's brittle on version updates and could break in the future. You can request a feature here: https://github.com/serilog/serilog/issues – C. Augusto Proiete Nov 05 '20 at 01:54