serilog-settings-appsettings icon indicating copy to clipboard operation
serilog-settings-appsettings copied to clipboard

Dynamic Log Filenames

Open ltoshea opened this issue 5 years ago • 7 comments

I currently am writing to a statically named log file as defined in my serilog config: <add key="serilog:write-to:File.path" value="%LOCALAPPDATA%\App_Data\Logs\StaticallyNamedLog.log" /> I'd like to be able to change the logfile name at runtime i.e.

<add key="serilog:write-to:File.path" value="%LOCALAPPDATA%\App_Data\Logs\{appname}-Log.log" />

In log4net for example I'd set a log4net global property value for "appname" in code before the settings were read in. This would then get substituted into this path.

    log4net.globalContext.properties["appname"] = "App1"
    log4net.config.xmlConfigurator.configure("log4net.config")

Then in the logfile I'd reference it like so

%property{appname}

This would then get substituted into this path.

I've really tried to find a solution to this but to no avail - can someone offer any advice as to how I might achieve this?

ltoshea avatar Jul 29 '19 16:07 ltoshea

You can use environment variables in Serilog.Settings.AppSettings values, so if you call System.Environment.SetEnvironmentVariable("APPNAME", "Test") at start-up, before reading from configuration, you can use %APPNAME% in the configured file path. HTH!

nblumhardt avatar Jul 29 '19 22:07 nblumhardt

Thanks for your suggestion nblumhardt, this does seem like a possible solution. How about applications that are running in a context that are more confined and cannot set environmental variables. Or if you are using this as a logging class library. Each application using this would set its own environmental variable value to 'appname' and you would get in all kinds of mess.

ltoshea avatar Jul 30 '19 06:07 ltoshea

Thanks for the reply. The same kinds of issues apply to the log4net version, don't they?

I can guess that in some *nix environments SetEnvironmentVariable() might be restricted, but I don't have any examples at hand.

Does this approach work for your current scenario?

nblumhardt avatar Jul 31 '19 01:07 nblumhardt

Unfortunately not, I'm try to use serilog to provide a logging service that is used by multiple applications and would like a single unified configuration. In log4net the 'appname' property would be attached to the context of each logger meaning I can have a singular config across loggers that write to multiple files. If I understand correctly what you mean by an environmental variable, it seems with the current suggestion each logger would overwrite the previous value.

ltoshea avatar Jul 31 '19 09:07 ltoshea

I'm not sure we're on the same page; by definition, wouldn't log4net.globalContext.properties["appname"] = "App1" also overwrite earlier values?

nblumhardt avatar Aug 02 '19 10:08 nblumhardt

I don't believe so, in the case I'm describing each application has it's own log4net context - so each app could have a property called "appname" with different value (i.e. app1, app2, app3 - which would mean logging to three separate log files) They could all share the same configuration file that would have %property{Appname} in.

If we set an environment variable as suggested for serilog it's system wide - so all loggers would pick up this one value, regardless of if they are different applications.

Hopefully I'm making sense (and I'm not incorrect). Thanks, Liam

ltoshea avatar Aug 04 '19 18:08 ltoshea

Ah, I think this is where we're misunderstanding each other. Environment.SetEnvironmentVariable() is not system-wide, it's process-wide. HTH!

nblumhardt avatar Aug 04 '19 21:08 nblumhardt