i know there are some log levels builtin in NLog like trace, info, fatal etc
i want to define some new ones such as "DBLog" and be able to only configure all logs with DBlog to be targeted to a certain Target.
is there is a way to do that ? or i have to define a custom log ?
Writing Log Messages With NLog The available methods for logging are (in ascending order): Trace, Debug, Info, Warn, Error, and Fatal. Each of these methods creates a log message with a corresponding log level—an indicator of the message's importance.
How do you use NLog for multiple projects in the same solution? If you want to log in other projects within the solution, just reference NLog. dll directly in each of the projects and use it. But now you only need to configure NLog in your main application.
NLog is “a free logging platform for . NET, Silverlight and Windows Phone with rich log routing and management capabilities. It makes it easy to produce and manage high-quality logs for your application regardless of its size or complexity.”
@SemVanmeenen is right, NLog does not allow for adding custom levels (that is, extending the possible values in the LogLevel class). Plus, given the way that levels work, at least in NLog, each level generally indicates the priority of the message. So, you if you have some (or all) loggers configured to log only Error and higher level messages, any Trace, Debug, Info, and Warn messages will not get logged. Where would a DBLog level fit? It seems like that would be more of a "category" (which can be somewhat analogous to logger names) rather than a level.
Do you really need a special level (or more than one) to log stuff like "DBLog"? Why not just define another logger in your config file called "DBLog"? That way you could use filtering and Targets to send those logs to a specific Target or turn that logger on or off. See this link for some examples of stuff you can do with NLog configuration.
One of the most common patterns in NLog and log4net is to have a logger per class, like this:
class MyClass
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public void DoSomething(int x, int y);
{
logger.Info("Doing something. x = {0}, y = {1}", x, y);
//Or
logger.Log(LogLevel.Info, "Doing something. x = {0}, y = {1}", x, y);
}
}
You can also use arbitrary logger names:
class MyDbClass
{
private static readonly Logger logger = LogManager.GetLogger("DbStuff");
public void DoSomething(int x, int y);
{
logger.Info("Doing something. x = {0}, y = {1}", x, y);
//Or
logger.Log(LogLevel.Info, "Doing something. x = {0}, y = {1}", x, y);
}
}
class MyOtherDbClass
{
private static readonly Logger logger = LogManager.GetLogger("DbStuff");
public void DoSomething(int x, int y);
{
logger.Info("Doing something. x = {0}, y = {1}", x, y);
//Or
logger.Log(LogLevel.Info, "Doing something. x = {0}, y = {1}", x, y);
}
}
In the second example, both classes are using the same logger ("DbStuff") and both can be controlled (level of logging, Target, etc) based on that logger name.
You could also use different loggers within the same class (maybe by default you use the "per class" logger style, but you also define some "cross cutting" loggers to make it easier to log - and control - similar information across classes). You might have a logger called "SQL" or "PERFORMANCE" that you use occasionally to log specific information that you would like to control at a level other than class level.
Using different logger names you can certainly route logging messages to different targets without defining new log levels.
If this doesn't help, maybe you could elaborate in your question about exactly what you want to do. What kind of logging code do you wish you could write that you feel you can't write now?
[UPDATE]
<logger name="Name.Space.Class1" minlevel="Debug" writeTo="f1" />
<logger name="Name.Space.Class1" levels="Debug,Error" writeTo="f1" />
<logger name="Name.Space.*" writeTo="f3,f4" />
<logger name="Name.Space.*" minlevel="Warn" writeTo="f3,f4" />
<logger name="Name.Space.*" minlevel="Debug" maxlevel="Error" final="true" />
Assume that f1, f2, f3, and f4 are File targets.
Line 1 will cause the logger for Class1 to write to the f1 target for Debug and higher level messages (i.e. Trace will be skipped).
Line 2 will cause the logger for Class1 to write to the f1 target ONLY Debug and Error level messages (i.e. Trace, Info, Warn, and Fatal will be skipped)
Line 3 will cause the loggers for all classes in namespace Name.Space to write to targets f3 and f4 regardless of level.
Line 4 will cause the loggers for all classes in namespace Name.Space to write to targets f3 and f4 only if the log level is Warn or higher (i.e. Trace, Debug, and Info will be skipped)
Line 5 will cause the loggers for all classes in namespace Name.Space to reject any messages whose level is between Debug and Error. They are rejected because there is no writeTo clause. No further rules are processed for the loggers because of final=true
These examples show how to filter at the logger level. You can do further filtering at the Target level using the FilteringTargetWrapper. I haven't used it, but there is an example of configuring a FilteringTargetWrapper here:
Most useful NLog configurations
I got these examples out of the Help file that came with the NLog 1.0 refresh install. I have not installed NLog 2.0 yet because I am in the middle of a project and prefer to wait until NLog 2.0 is out of beta before I move to it. If you are using NLog 2.0 and the help file doesn't have better examples than are on the NLog website, you might consider installing at least the NLog 1.0 help. You can get the NLog 1.0 help here: http://nlog.codeplex.com/releases/view/32601
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With