Is this the correct way to log to a specific target with NLog?

t3chb0t

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.

SaneDeveloper

You always can create another logger instance where you want with specified name of target. What is the problem?

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

    <rules>
<logger name="ExtendedLogging" minlevel="Trace" writeTo="extendedLogging"/>
    </rules>

    <targets>
     <target name="extendedLogging" >
      <target xsi:type="File" fileName="C:/Logs/ExtendedLog${shortdate}.log" createDirs="true" />
    </target>
    </targets>

Then go to your 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

NLog log to a specific target mentioned in the config

The correct way to ignore log entries with NLog at runtime

Nlog Concat to write to specific log

Log get all properties in custom NLog Target

Choosing an NLog target based on global log level

NLog: how to obtain level of a specific target programatically

NLog try another target when main target failed to log

NLog - create log file at specific directory in linux

Correct way to target and style specific child elements in Material-ui wrapper components?

NLog Log writing to different after changing filename of a target

Nlog set Database target programatically and access custom log message properties

NLog with Application Insights target doesn't log custom params and exceptions

Is there a way to programmatically configure target rules filters in NLog 4.6?

The correct way to log, and where to log, in PHP

Is there a way to log program specific behavior?

NLog Mail Target is not working

HttpClient in an NLog target

NLog Custom Target for JSNLog

Nlog Posting to a CosmosDB Target

A correct/common way to log async calls

Correct way to log events in another layer on ServiceStack

What is the best way to correct parse log file?

Is it correct way to log the open connections using CachingClientConnectionFactory

Correct way to restrict functions to specific data types

Correct way to introduce if statement to specific for loop?

What is the correct way for the anchor tag's target attribute?

Correct way to target button elements inside jquery element?

Nlog log to another directory

NLog multiple log factories