16

I'd like to log some special events into a different table that will contain more data then the general application log.

If I add a second database target to the NLog.config how can I use it in my code?

Would this be the right thing to do:

NLog
    .LogManager
    .Configuration
    .AllTargets
    .Single(x => x.Name == "mySecondLogTable")
    .WriteAsyncLogEvent(...);

Then in the NLog.config I would just skip this target in the rules element, right?


Summary: I'd like to define multiple database targets like a general log and specialized log for cases where I need to log more details into a different table. I'd like to use the general log by default and the special log only in functions that need this special kind of logging because of their business logic.

t3chb0t
  • 16,340
  • 13
  • 78
  • 118
  • I'd use only config settings and no source code. The config file is a mix of Targets & Rules. You can use it to control where the entries are written. – Fabrizio Accatino Mar 10 '15 at 09:05
  • Agreeing with @FabrizioAccatino. XML configurations may seem daunting at first, but I highly recommend it for NLog. It makes the process much more understandable, and you can easily make highly configurable targets/rules. – Flater Mar 10 '15 at 09:08
  • Yes, I also prefer XML configurations but I found only this so I thought I'd better ask if this is ok because I had the feeling I'm doing something terribly wrong. – t3chb0t Mar 10 '15 at 09:15

2 Answers2

29

You can always create another logger instance and use the NLog LoggingRules for redirection to the wanted target.

For example I want to make an extended logging into a separate file. Then I go and create:

<nlog>
  <rules>
    <!--- Notice that final=true stops the logevents from also reaching defaultTarget -->
    <logger name="ExtendedLogging" minlevel="Trace" writeTo="extendedTarget" final="true" />
    <!--- Wildcard rule will capture all logevents not matching the "final" rule above -->
    <logger name="*" minlevel="Trace" writeTo="defaultTarget" />
  </rules>
    
  <targets>
    <target name="extendedTarget" xsi:type="File" fileName="ExtendedLog_${shortdate}.log" />
    <target name="defaultTarget" xsi:type="File" fileName="AppLog_${shortdate}.log" />
  </targets>
</nlog>

And then I go to the code and create

private readonly Logger logger = LogManager.GetLogger("ExtendedLogging");

I don't think it's a good idea to search for something inside the config-file and perform logging through something like a backdoor. It's better to make all this things explicitly.

See also: https://github.com/nlog/nlog/wiki/Configuration-file#rules

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
EngineerSpock
  • 2,575
  • 4
  • 35
  • 57
  • The problem is that I didn't know it was possible ;-) until your answer. I've never had the luck to find out that I can define a named logger in the config. I didn't know that I if specify an existing logger name to the `GetLogger` method it would give me that logger. I think this is what I was looking for. I'll try it out. – t3chb0t Mar 10 '15 at 09:13
  • Then you can mark my reply as the answer! :) It works for me, it will likely work for you))) Just try that. – EngineerSpock Mar 10 '15 at 09:23
  • I definitely will... just give me some time to test it ;-] I don't like working unmarked answers so I won't forget you ;-) – t3chb0t Mar 10 '15 at 09:26
  • Have you tested this solution? – EngineerSpock Mar 11 '15 at 11:28
  • I have two targets, `email` and `file`, when I do this the logger still uses both – Toolkit Feb 01 '21 at 10:23
  • 1
    @Toolkit Updated the answer with more explanation of how to use NLog LoggingRules – Rolf Kristensen Feb 01 '21 at 11:00
1
    var fileLogger = LogManager.Configuration.AllTargets.Single(x => x.Name == "file");
    fileLogger.WriteAsyncLogEvents(
                            new LogEventInfo(LogLevel.Info, null, error.ToString())
                            .WithContinuation(new NLog.Common.AsyncContinuation(_ => { })));

I am not sure what did I do but it works. Because the accepted solution didn't

Toolkit
  • 10,779
  • 8
  • 59
  • 68
  • 1
    Fixed the formatting of the accepted answer, so one can actually see the `NLog.config`-xml. And also added `final="true"` so the logging redirected to `extendedTarget` will not flow to `defaultTarget`. – Rolf Kristensen Feb 01 '21 at 10:56