sidebar_position | sidebar_label |
---|---|
10 |
How to use and configure FileLogProvider |
The FileLogProvider
can be instantiated in one of two ways.
file
- Type: string
- Description: The path to a writable file.
template
- Type: string
- Description: The template used to format log entries. See TemplateLogFormatter for more information.
level
- Type: LogLevel enum (NotSet, Debug, Info, Warn, Error, Fatal, Audit)
- Description: The level of the log provider. If
NotSet
, the level of the containingLogger
is used.
timeout
- Type: Nullable<TimeSpan>
- Description: The timeout of the log provider.
file
- Type: string
- Description: The path to a writable file.
formatter
- Type: ILogFormatter
- Description: An object that formats log entries prior to writing to standard out. See ILogFormatter for more information.
level
- Type: LogLevel enum (NotSet, Debug, Info, Warn, Error, Fatal, Audit)
- Description: The level of the log provider. If
NotSet
, the level of the containingLogger
is used.
timeout
- Type: Nullable<TimeSpan>
- Description: The timeout of the log provider.
To add a FileLogProvider
to a logger, call one of the five AddFileLogProvider
extension method overloads. Four of the overloads are very similar, differing only by how the ILogFormatter
is specified.
public void ConfigureServices(IServiceCollection services)
{
services.AddLogger()
.AddFileLogProvider(
file: "C:\\my\\path\\log.txt",
template: "{level}: {message}",
level: LogLevel.Info,
timeout: TimeSpan.FromSeconds(1));
}
public void ConfigureServices(IServiceCollection services)
{
IDependency dependency = new MyDependency();
int parameter = 12345;
ILogFormatter formatter = new CustomLogFormatter(dependency, parameter);
services.AddLogger()
.AddFileLogProvider(
file: "C:\\my\\path\\log.txt",
formatter: formatter,
level: LogLevel.Info,
timeout: TimeSpan.FromSeconds(1));
}
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IDependency, MyDependency>();
services.AddLogger()
.AddFileLogProvider<CustomLogFormatter>(
file: "C:\\my\\path\\log.txt",
level: LogLevel.Info,
timeout: TimeSpan.FromSeconds(1),
logFormatterParameters: 12345);
}
public void ConfigureServices(IServiceCollection services)
{
services.AddLogger()
.AddFileLogProvider(
file: "C:\\my\\path\\log.txt",
formatterRegistration: serviceProvider => new CustomLogFormatter(new MyDependency(), 12345),
level: LogLevel.Info,
timeout: TimeSpan.FromSeconds(1));
}
public void ConfigureServices(IServiceCollection services)
{
services.AddLogger()
.AddFileLogProvider(options =>
{
options.File = "C:\\my\\path\\log.txt";
// Could also set the formatter with one of the other
// SetFormatter overloads or the SetTemplate method.
options.SetFormatter<CustomLogFormatter>(12345);
options.Level = LogLevel.Info;
options.Timeout = TimeSpan.FromSeconds(1);
});
}
{
"Rocklib.Logging": {
"Providers": [
{
"Type": "RockLib.Logging.FileLogProvider, RockLib.Logging",
"Value": {
"File": "C:\\my\\path\\log.txt",
"Template": "{level}: {message}",
"Level": "Info",
"Timeout": "00:00:01"
}
}
]
}
}
{
"Rocklib.Logging": {
"Providers": [
{
"Type": "RockLib.Logging.FileLogProvider, RockLib.Logging",
"Value": {
"File": "C:\\my\\path\\log.txt",
"Formatter": {
"Type": "MyAssembly.CustomLogFormatter, MyAssembly",
"Value": {
"Dependency": {
"Type": "MyAssembly.MyDependency, MyAssembly"
},
"Parameter": 12345
}
},
"Level": "Info",
"Timeout": "00:00:01"
}
}
]
}
}