adams85 / filelogger

A lightweight yet feature-rich file logger implementation for the Microsoft.Extensions.Logging framework.
MIT License
147 stars 22 forks source link

Path variable <appname> #7

Closed GGY-CM closed 4 years ago

GGY-CM commented 4 years ago

Maybe add a new variable like in the Path, and replace it to current application name in code?

Thanks,

adams85 commented 4 years ago

The sample I mentioned in your other issue shows a possible solution to this.

However, application name is static (I mean it doesn't change during a single run), so you can get along even without subclassing:

builder.AddConfiguration(configuration.GetSection("Logging"));

builder.AddFile(o =>
{
    /* ... */
    var appName = Assembly.GetEntryAssembly().GetName().Name;
    foreach (var file in o.Files)
        file.Path = file.Path.Replace("<appname>", appName);
});

That is, there are already at least two easy ways to achieve this behavior, therefore I don't think it's worth supporting this out-of-the-box.

adams85 commented 3 years ago

Version 3.2.0 introduces a setting which properly handles this without the need of subclassing:

builder.AddFile(o =>
{
    o.RootPath = AppContext.BaseDirectory;

    var appName = Assembly.GetEntryAssembly().GetName().Name;
    o.PathPlaceholderResolver = (placeholderName, _, __) => placeholderName == "appname" ? appName : null;
});