NLog - Advanced .NET, .NET Standard, Silverlight and Xamarin Logging

Most useful NLog configurations [closed]

What are the best or most useful configurations for logging with NLog? (These can be simple or complex, as long as they're useful.)

I'm thinking of examples like automatically rolling over log files at a certain size, changing the layout (log message) whether or not there is an exception, escalating the log level once an error has occurred, etc.

Here are some links:

Source: (StackOverflow)

NLog time formatting

How do I write a layout for NLog that outputs time with milliseconds like this 11:32:08:123? I use ${date:format=yyyy-MM-dd HH\:mm\:ss} but I need more time precision in my logs.

Source: (StackOverflow)

Delete log files after x days

would like to log with Nlog and the file target like in this example. How can I realize a deletion of the files after x days without archiving them. Or it is possible to archive the files in the same folder?

Source: (StackOverflow)

NLog: How to exclude specific loggers from a specific rule?

In my NLog configuration, I have a catch-all logger but a specific logger I have created is very spammy and I want its output to go to its own file. That part is easy, but the catch-all logger receives the spammy log messages as well. How do I tell the main logger to log everything but to exclude the spammy logger?

I'm using NLog 2.0.

Source: (StackOverflow)

Why do loggers recommend using a logger per class?

As per NLog's documentation:

Most applications will use one logger per class, where the name of the logger is the same as the name of the class.

This is the same way that log4net operates. Why is this a good practice?

Source: (StackOverflow)

Dependency injection and named loggers

I am interested in learning more about how people inject logging with dependency injection platforms. Although the links below and my examples refer to log4net and Unity, I am not necessarily going to use either of those. For dependency injection/IOC, I will probably use MEF as that is the standard that the rest of the project (large) is settling on.

I am very new to dependency injection/ioc and am pretty new to C# and .NET (have written very little production code in C#/.NET after the past 10 years or so of VC6 and VB6). I have done a lot of investigation into the various logging solutions that are out there, so I think that I have a decent handle on their feature sets. I am just not familiar enough the with actual mechanics of getting one dependency injected (or, maybe more "correctly", getting an abstracted version of one dependency injected).

I have seen other posts related to logging and/or dependency injection like: dependency injection and logging interfaces

Logging best practices

What would a Log4Net Wrapper class look like?

again about log4net and Unity IOC config

My question does not have specifically to do with "How to I inject logging platform xxx using ioc tool yyy?" Rather, I am interested in how people have handled wrapping the logging platform (as is often, but not always recommended) and configuration (i.e. app.config). For example, using log4net as an example, I could configure (in app.config) a number of loggers and then get those loggers (without dependency injection) in the standard way of using code like this:

private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

Alternatively, if my logger is not named for a class, but rather, for a functional area, I could do this:

private static readonly ILog logger = LogManager.GetLogger("Login");
private static readonly ILog logger = LogManager.GetLogger("Query");
private static readonly ILog logger = LogManager.GetLogger("Report");

So, I guess that my "requirements" would be something like this:

  1. I would like to insulate my product's source from a direct dependency on a logging platform.

  2. I would like to be able to resolve a specific named logger instance (probably sharing the same instance among all requesters of the same named instance) either directly or indirectly by some kind of dependency injection, probably MEF.

  3. I don't know if I would call this a hard requirement, but I would like the ability to get a named logger (different than the class logger) on demand. For example, I might create a logger for my class based on the class name, but one method needs particulary heavy diagnostics that I would like to control separately. In other words, I might want a single class to "depend" on two separate logger instances.

Let's start with number 1. I have read a number of articles, primarily here on stackoverflow, about whether or not it is a good idea to wrap. See the "best practices" link above and go to jeffrey hantin's comment for one view about why it is bad to wrap log4net. If you did wrap (and if you could wrap effectively) would you wrap strictly for the purpose of injection/removal of direct depdency? Or would you also try to abstract away some or all of the log4net app.config information?

Let's say I want to use System.Diagnostics, I would probably want to implement an interface-based logger (maybe even using the "common" ILogger/ILog interface), probably based on TraceSource, so that I could inject it. Would you implement the interface, say over TraceSource, and just use the System.Diagnostics app.config information as is?

Something like this:

public class MyLogger : ILogger
  private TraceSource ts;
  public MyLogger(string name)
    ts = new TraceSource(name);

  public void ILogger.Log(string msg)

And use it like this:

private static readonly ILogger logger = new MyLogger("stackoverflow");
logger.Info("Hello world!")

Moving on to number 2 ... How to resolve a particular named logger instance? Should I just leverage the app.config information of the logging platform that I choose (i.e. resolve the loggers based on the naming scheme in the app.config)? So, in the case of log4net, might I prefer to "inject" LogManager (note that I know this is not possible since it is a static object)? I could wrap LogManager (call it MyLogManager), give it an ILogManager interface, and then resolve MyLogManager.ILogManager interface. My other objects could have a depenency (Import in MEF parlance) on ILogManager (Export from the assembly where it is implemented). Now I could have objects like this:

public class MyClass
  private ILogger logger;
  public MyClass([Import(typeof(ILogManager))] logManager)
    logger = logManager.GetLogger("MyClass");

Any time ILogManager is called, it would directly delegate to log4net's LogManager. Alternatively, could the wrapped LogManager take the ILogger instances that it gets based on the app.config and add them to the(a ?) MEF container by name. Later, when a logger of the same name is requested, the wrapped LogManager is queried for that name. If the ILogger is there, it is resolved that way. If this is possible with MEF, is there any benefit do doing so?

In this case, really, only ILogManager is "injected" and it can hand out ILogger instances in the way that log4net normally does. How does this type of injection (essentially of a factory) compare to injecting the named logger instances? This does allow for more easy leveraging of log4net's (or other logging platform) app.config file.

I know that I can get named instances out of the MEF container like this:

var container = new CompositionContainer(<catalogs and other stuff>);
ILogger logger = container.GetExportedValue<ILogger>("ThisLogger");

But how do I get the named instances into the container? I know about the attribute based model where I could have different implementations of ILogger, each of which is named (via a MEF attribute), but that doesn't really help me. Is there a way to create something like an app.config (or a section therein) that would list the loggers (all of the same implementation) by name and that MEF could read? Could/should there be a central "manager" (like MyLogManager) that resolves named loggers via the underlying app.config and then inserts the resolved logger into the MEF container? This way it would be available to someone else that has access to the same MEF container (although without the MyLogManager's knowledge of how to use log4net's app.config information, it seems that the container would be unable to resolve any named loggers directly).

This has already gotten pretty long. I hope it that it is coherent. Please feel free to share any specific information about how you dependency injected a logging platform (we are most likely considering log4net, NLog, or something (hopefully thin) built on System.Diagnostics) into your application.

Did you inject the "manager" and have it return logger instances?

Did you add some of your own config information in your own config section or in your DI platform's config section to make it easier/possible to inject logger instances directly (i.e. make your dependencies be on ILogger rather than ILogManager).

What about having a static or global container that has either the ILogManager interface in it or the set of named ILogger instances in it. So, rather than injecting in the conventional sense (via constructor, property, or member data), the logging dependency is explicitly resolved on demand. Is this a good or bad way to dependency inject.

I am marking this as a community wiki since it doesn't seem like a question with a definite answer. If anyone feels otherwise, feel free to change it.

Thanks for any help!

Source: (StackOverflow)

log4net vs. Nlog

Anyone have experience for both? How do they stack up against each other?

We are planning on using one of them for logging in an enterprise application.




EDIT: We have no existing dependencies to either nlog or log4net.

Source: (StackOverflow)

Logger wrapper best practice

I want to use a nlogger in my application, maybe in the future I will need to change the logging system. So I want to use a logging facade.

Do you know any recommendations for existing examples how to write those ones ? Or just give me link to some best practice in this area.

Source: (StackOverflow)

How can I output NLog stuff to the vs2008 'output' window?

I'm using NLog to log my stuff. I'm trying to send the output to the console (or colouredconsole) ... which I'm hoping would go to the Visual Studio 'OUTPUT' window for any ASP.NET web site/app/mvc app.

It's not. If I change the target to 'file' then it works for sure.

Can NLog output to the 'output' window for web apps?

Source: (StackOverflow)

using AppData location in NLog

My NLog targets is like this:

  <target xsi:type="Console" name="console" 
    layout="${longdate}|${level}|${message}" />
  <target xsi:type="File" name="ErrorLog" fileName="${basedir}/error.txt"
          Trace: ${stacktrace} 
          ${message}" />
  <target xsi:type="File" name="AccessLog" fileName="${basedir}/access.txt"
          layout="${shortdate} | ${message}" />

But this causes problems if the user isn't an admin on their machine, because they will not have write access to "Program Files". How can I get something like %AppData% to NLog instead of BaseDir?

Source: (StackOverflow)

How to get the Stack trace when logging exceptions with NLog?

When I use the default layout with NLog it only prints the name of the exception. I've been told that the log4jxmlevent layout doesn't prints nothing about the exception. What layout will help me?

Example code:

    throw new SystemException();
catch (Exception ex)
    logger.Error("oi", ex);

Default layout output:

2011-01-14 09:14:48.0343|ERROR|ConsoleApplication.Program|oi

log4jxmlevent output:

<log4j:event logger="ConsoleApplication.Program"
<log4j:NDC />
<log4j:locationInfo class="ConsoleApplication.Program"
                    method="Void Main(System.String[])"
                    file="C:\Users\User\Documents\Visual Studio 2010\Projects\ConsoleApplication\ConsoleApplication\Program.cs"
                    line="21" />
<nlog:locationInfo assembly="ConsoleApplication, Version=, Culture=neutral, PublicKeyToken=null" />
  <log4j:data name="log4japp"
              value="true" />
  <log4j:data name="log4jmachinename"
              value="MACHINE" />

Source: (StackOverflow)

How to get NLog to write to database

I'm trying to get NLog to log to my database log table but to no avail. I'm sure my connection string is correct because it's the same used elsewhere in my web.config. Writing out to a file works fine, so I know it's not just NLog, but must be something I'm doing wrong. Below is my NLog configuration:

  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <target name="file" xsi:type="File" fileName="${basedir}/logs/Log ${shortdate}.txt" layout="${longdate} ${callsite} ${level}: ${message} ${exception:format=Message,StackTrace} ${stacktrace}" />
      <target type="Database" name="database" connectionstring="MyConnectionString">
          insert into MyLog ([CreateDate], [Origin], [LogLevel], [Message], [Exception], [StackTrace]) values (@createDate, @origin, @logLevel, @message, @exception, @stackTrace);
        <parameter name="@createDate" layout="${longdate}"/>
        <parameter name="@origin" layout="${callsite}"/>
        <parameter name="@logLevel" layout="${level}"/>
        <parameter name="@message" layout="${message}"/>
        <parameter name="@exception" layout="${exception:format=Message,StackTrace}"/>
        <parameter name="@stackTrace" layout="${stacktrace}"/>
      <logger name="*" writeTo="file"/>
      <logger name="*" appendTo="database"/>
      <!--<logger name="*" writeTo="mail" minlevel="Error"/>-->

Source: (StackOverflow)

NLog performance

What should the expected overhead be for logging? I have tried this example

 private class Person
    private static Logger logger = LogManager.GetCurrentClassLogger();
    public string Name { get; private set; }
    public Person(string name)
           Name = name;
           logger.Info("New person created with name {0}", name);

  List<Person> people = new List<Person>();
  for (int i = 0; i < MAXTEST; i++)
      people.Add(new Person(i.ToString()));

With MAXTEST values of 100,500,1000, 5000

Results in MAXTEST,noLogging, Logging

100,  25ms, 186ms    
500,  33ms, 812ms    
1000, 33ms, 1554ms
5000, 33ms, 7654ms

Granted one would probably never log this excessive amount, but it this the performance hit one would expect?

I have also tried using the asyncwrapper in the config

 <target name="asyncFile" xsi:type="AsyncWrapper">
   <target name="file" xsi:type="File" fileName="${basedir}/log.txt" />



Source: (StackOverflow)

Global exception handling in Web API 2.1 and NLog

In Web API 2.1 is new Global Error Handling. I found some example how to log exceptions into Elmah ( elmah sample ). But I use NLog to log errors into database table. Is it posible to use Web API Global Error Handling with NLog? Please provide some example.

Source: (StackOverflow)

When should I use Tracing vs Logger.NET, Enterprise Library, log4net or Ukadc.Diagnostics?

How do I choose between standard tracing, Logger.NET, Enterprise Library, log4net or Ukadc.Diagnostics?

Is there a situation where one is more appropriate than the other? ... what would that be? (ASP.NET, console app, Azure Cloud, SOHO, Enterprise...)

What are the benefits or drawbacks?

Did I miss any other major logging frameworks?

Source: (StackOverflow)