I have a class that should be logging as usual, but sometimes it should be logging to a specific target(eg. a file).
Here are my NLog config files:
nlog.config
<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogFile="C:\Logs\logfile.log"
internalLogLevel="Info" >
<targets>
<target xsi:type="File" name="logfile" fileName="C:\Logs\logfile.log"
layout="${longdate}|${level}|${message} |${all-event-properties} ${exception:format=tostring}" />
<target xsi:type="Console" name="logconsole"
layout="${longdate}|${level}|${message} |${all-event-properties} ${exception:format=tostring}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile,logconsole" />
</rules>
</nlog>
and extra.config
<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogFile="C:\Logs\extra.log"
internalLogLevel="Info" >
<targets>
<target xsi:type="File" name="extra" fileName="C:\Logs\extra.log"
layout="${longdate}|${level}|${message} |${all-event-properties} ${exception:format=tostring}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="extra" />
</rules>
</nlog>
I am trying something like this:
public class Program
{
private static IConfiguration _configuration;
public static void Main(string[] args)
{
var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
logger.Debug("init main");
var environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production";
_configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json", false)
.AddJsonFile($"appsettings.{environment}.json", true)
.Build();
CreateHostBuilder(args).Build().Run();
}
catch (Exception exception)
{
//NLog: catch setup errors
logger.Error(exception, "Stopped program because of exception");
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
NLog.LogManager.Shutdown();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host
.CreateDefaultBuilder(args)
.ConfigureLogging(logBuilder =>
{
logBuilder.ClearProviders();
logBuilder.SetMinimumLevel(LogLevel.Trace);
})
.ConfigureServices((hostContext, services) =>
{
//serviceConfig
})
.UseNLog()
.UseWindowsService();
}
And the class I want two loggers in:
public class MyClass
{
private readonly ILogger<MyClass> _logger;
private readonly Logger _extraLogger;
public MyClass(ILogger<MyClass> logger)
{
_logger = logger;
_extraLogger = NLogBuilder.ConfigureNLog("extra.config").GetLogger("extra");
}
public void doBothTypesOfLogging()
{
var msg = "Hello";
_extraLogger.Info(msg);
_logger.LogInformation("Message sendt");
}
}
But this makes all logging done by the application(even other classes) end up in the extra.log
file which is defined by the extra.config. Eg. both msg
AND "Message sendt"
ends up in that file.
What i want is to have
"Message sendt"
in logfile.log
msg
in extra.log
Is this possible somehow?