Reputation: 13292
I am probably missing a big point here, but I understand that loggers are defined in NLog.config
and that targets also are defined in NLog.config
.
I am using a logger defined with the name "*" as my logger. So when I want to log an error, I do something like this:
Logger.Log.Error("Oops.");
That code writes to a target that my logger specifies to use for level "Error". It writes to an file called "Error.log". Fine.
For any of my code that affects the UI (this code is peppered across multiple classes), I sometimes want to log to a UI.txt file. So I have created a logger called "UI" and it specifies a writeTo value that corresponds to a defined target. Fine.
Ahead of my .Log method call, I use the GetLogger method to get the UI logger:
NLog.LogManager.GetLogger("UI");
Then when I use this command, it writes to the UI target (a file called UI.log):
Logger.Log.Error("Oops.");
My question is: How do I put the logger back to its previous value so that the next time I use the Logger.Log.Error method it writes to Error.log instead of to UI.log? I expect that I will need to use another line like this, but I don't know what to put in place of the ???:
NLog.LogManager.GetLogger("???");
I consulted the documentation and did not see anything that would help me understand what argument to pass to GetLogger to switch back to the default logger. Even if passing "*" works, it might do more than I need/want it to do. Is there just a way to get the "default" logger that's in effect before you ever call the GetLogger() method?
Upvotes: 0
Views: 7207
Reputation: 540
The way I understand NLog is that in the config you specify targets (whereto to send the log) and layout (the format for the log message) and then specify rules to direct log messages from specific loggers to specific targets.
The LogManager.GetCurrentClassLogger()
retrieves a singleton Logger with the name of the current class. LogManager.GetLogger("UI")
would retrieve a singleton Logger with the name "UI".
In the NLog.config rules you can direct all messages from the logger with name="UI" to the UI log target and all others name="*" (wildcard name) to a default target. Note that if want the UI log not to go to the default target you can set it as final.
In this case, you would instantiate the two loggers in your class and when you log a message, just use the required instance.
However, it seems as if you want to decide which logger to use at runtime and thus always want to use the same logger instance to log to. In that case, you can simply add a static method to pick the right instance based on some condition
static ILogger DefaultLogger = LogManager.GetCurrentClassLogger();
static ILogger UiLogger = LogManager.GetLogger("UI);
static ILogger ActiveLogger
{
get{ return condition == A?DefaultLogger:UiLogger;}
}
Now you can simply write ActiveLogger.LogError("Oops.")
Upvotes: 0
Reputation: 36770
I am probably missing a big point here, but I understand that loggers are defined in NLog.config and that targets also are defined in NLog.config.
This isn't fully correct. In the NLog.config targets and rules are defined, not loggers.
You will have something like this probably:
<rules>
<logger name="*" writeTo="myTarget" />
</rules>
Which means, define a rule which mathes a logger with name matching the wildcard *
.
My question is: How do I put the logger back to its previous value
Or save the Logger
instance, or maybe use LogManager.GetCurrentClassLogger()
? Please also note that the Logger
has a Name
property.
Please note also is that "put back" doesn't sound thread-safe. If you need thread-safeness, separate logger instances (as suggested by Antonios Katopodis) is recommend.
Upvotes: 2
Reputation: 1537
I would highly recommend creating 2 static classes UILog
and DefaultLogger
where you will simply call them as UILog.Log("your_message")
and the log class will be like this:
private static Logger logger = LogManager.GetLogger("your_logger");
public static void Log(string message){
Logger.Log.Error(message);
}
I think that it's a good practice to have loggers with names and create classes for each of those loggers with the appropriate methods.
Upvotes: 2