Reputation: 1058
I need to change the layout of my log file when writing log entries from an aspect. I will likely need to change other settings as well in time.
I have created two FileTargets that create the layout as I require.
LoggingConfiguration config = new LoggingConfiguration();
LoggingConfiguration aspectConfig = new LoggingConfiguration();
FileTarget fileTarget = new FileTarget();
fileTarget.Name = "fileTarget";
fileTarget.Layout = "${longdate} ${machineName} ${callsite} ${message} ${exception:format=tostring}";
fileTarget.FileName = String.Format("{0}Admin.log", Config.DatabasePath);
fileTarget.KeepFileOpen = false;
FileTarget aspectTarget = new FileTarget();
aspectTarget.Name = "aspectTarget";
aspectTarget.Layout = "${longdate} ${machineName} ${message} ${exception:format=tostring}";
aspectTarget.FileName = String.Format("{0}Admin.log", Config.DatabasePath);
aspectTarget.KeepFileOpen = false;
LoggingRule rule2 = new LoggingRule("*", LogLevel.Trace, fileTarget);
config.LoggingRules.Add(rule2);
LoggingRule aspectRule = new LoggingRule("aspects", LogLevel.Trace, aspectTarget);
aspectConfig.LoggingRules.Add(aspectRule);
LogManager.Configuration = config;
There may be other changes/differences to come but that is besides the point at this stage.
Using the following works fine for the 'default or '*' config ie by using :
var log = LogManager.GetCurrentClassLogger();
log.Fatal("##########################################");
What I want to do is be able to use the 'aspectRule' rather than the default '*' rule by calling logger and specifying the 'aspectRule'.
I had thought this would work:
var log = LogManager.GetLogger("aspects");
log.Fatal("########################################");
which is all well and good but I can't see how I define a new Logger called 'aspects' particularly as in code I don't even create a Logger called '*' :-)
Note that I am not using an XML config file and need a solution to use programatically.
Any ideas?
EDIT:
I need to write 1 log entry as in this example:
if (someCondition)
logger.Fatal("#############"); // use layout in fileTarget
else
logger.Fatal("##############"); // use layout in aspectTarget
Upvotes: 0
Views: 170
Reputation: 3496
EDIT: To answer your edited question, LoggingRule rule2 = new LoggingRule("*", LogLevel.Trace, fileTarget)
will log any output to the log file.
If you want a specific output then you'll have to be specific with NLog.
For example, log anything ranging from Trace
up to Info
with a fileTarget ("*"), and have two other specific targets aspects
and rule2
with log levels from Error
to Fatal
.
Then you could log like this:
LogManager.GetCurrentClassLogger().Debug( "Non-specific message" );
LogManager.GetLogger( "rule2" ).Fatal( "From rule2" );
LogManager.GetLogger( "aspects" ).Fatal( "From Aspects" );
--
You're creating two configurations config
and aspectConfig
, setting up both and eventually setting LogManager.Configuration = config
.
Eventually aspectConfig
is not being used. To resolve, remove all aspectConfig
related code and fix the acpects
line to this:
config.LoggingRules.Add(aspectRule);
Upvotes: 2