Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create the log file by process name? #411

Open
xuliujian opened this issue Jan 31, 2024 · 3 comments
Open

How to create the log file by process name? #411

xuliujian opened this issue Jan 31, 2024 · 3 comments

Comments

@xuliujian
Copy link

xuliujian commented Jan 31, 2024

I create a common trace library for other solutions. But I can not find a way to configure the path dynamicly.
I tried to create a enrich to add a property named InstanceName, but it does not work.

{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{TraceCategory} - {Timestamp:o} [{Level:u3}] {Message}{NewLine}"
}
},
{
"Name": "File",
"Args": {
"path": "./logs/{InstanceName}.log",
"outputTemplate": "{TraceCategory} - {Timestamp:o} [{Level:u3}] {Message}{NewLine}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithTraceCategory", "WithInstanceName" ],
"Destructure": [
{
"Name": "ToMaximumDepth",
"Args": { "maximumDestructuringDepth": 4 }
},
{
"Name": "ToMaximumStringLength",
"Args": { "maximumStringLength": 100 }
},
{
"Name": "ToMaximumCollectionCount",
"Args": { "maximumCollectionCount": 10 }
}
]
}
}

@bartelink
Copy link
Member

You're best off asking stuff like this on stackoverflow - anyone that watches this repo watches the serilog tag there - and be sure to take the time to format your question and include any information about messages you receive (search up about Serilog Selflog)

@cocowalla
Copy link

Enrichers don't work for filenames, only for log message templates.

I don't believe there is a way to name the log file based on the process name while using JSON config. Instead, I think you'll need to configure the loggewr in code instead. You probably already know this if you've tried writing an enricher, but you can get the process name (without the file extension) by using:

System.Diagnostics.Process.GetCurrentProcess().ProcessName

@xuliujian
Copy link
Author

@cocowalla Thank you. At the moment, I have a workaround to implement this. I add a PlaceHolder flag such as ${InstanceName} in the path, and then read the appsettings.json and replace the PlaceHolder with the process name, then LogConfiguration reads from the configuration. But I think it is not too nice.

public static IConfigurationRoot ApplyPlaceHolder(this IConfigurationRoot configuration, string placeHolder, string placeTarget)
{
    var writeToDirectiveSection = configuration.GetSection("Serilog:WriteTo");

    if (writeToDirectiveSection.GetChildren().Any())
    {
        foreach (var sink in writeToDirectiveSection.GetChildren())
        {
            var pathConfigSection = sink.GetSection("Args:path");

            if (pathConfigSection != null && !string.IsNullOrEmpty(pathConfigSection.Value))
            {
                if (pathConfigSection.Value.Contains(placeHolder))
                {
                    pathConfigSection.Value = pathConfigSection.Value.Replace(placeHolder, placeTarget);
                }
            }
        }
    }

    return configuration;
}
            builder = new ConfigurationBuilder();
            configurationRoot = builder.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json")).Build();

            processName = Process.GetCurrentProcess().ProcessName;

            //Replace InstanceName with processName
            configurationRoot.ApplyPlaceHolder("${InstanceName}", processName);

            _loggerConfiguration = new LoggerConfiguration();
            _loggerConfiguration.ReadFrom.Configuration(configurationRoot)

@xuliujian xuliujian reopened this Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants