Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/NLogPublic

Configure component logging

Rolf Kristensen edited this pageApr 29, 2023 ·10 revisions

The default approach for managing Loggers via the global singletonLogManager class in NLog. It works well for homogeneous applications where the main application and all the extension components are configured via a single configuration file.

By including the component class-namespace in the Logger-name (Happens automatically when usingNLog.LogManager.GetCurrentClassLogger()), then it is easy to configure NLog Logging-Rules to route to the wanted destination-target for the plugin (Ex.filename="Plugin-${logger}.log").

Isolated Logger Property

If the component is complex and uses many Logger-instances, then one can consider to also imbue the component Logger-instance with a common LogEvent-PropertyName:

varpluginLogger=NLog.LogManager.GetLogger(componentName).WithProperty("ComponentName",componentName);

Then it is possible to include the component-identifier in the target output. Ex${event-properties:ComponentName:whenEmpty=Core}

See also${event-properties}

Isolated LogFactory

But sometimes you want to create a separate logging configuration, just for a particular component, plugin or extension. Instead of usingLogManager (which is global to an application) then one can use isolatedLogFactory object-instance that is local to your component/plugin. It allows you to have multipleLogFactory-instances, where each have their own isolated logging configuration.

When creatingLogFactory you need to explicitly pass the configuration (either from a file or by programmatically constructing targets and rules) as in the following example:

internalclassMyLogManager{// A Logger dispenser for the current assembly (Remember to call Flush on application exit)publicstaticLogFactoryInstance{get{return_instance.Value;}}privatestaticLazy<LogFactory>_instance=newLazy<LogFactory>(BuildLogFactory);//// Use a config file located next to our current assembly dll// eg, if the running assembly is c:\path\to\MyComponent.dll// the config filepath will be c:\path\to\MyComponent.nlog//// WARNING: This will not be appropriate for assemblies in the GAC//privatestaticLogFactoryBuildLogFactory(){// Use name of current assembly to construct NLog config filenameAssemblythisAssembly=Assembly.GetExecutingAssembly();stringconfigFilePath=Path.ChangeExtension(thisAssembly.Location,".nlog");LogFactorylogFactory=newLogFactory();logFactory.Configuration=newXmlLoggingConfiguration(configFilePath,true,logFactory);returnlogFactory;}}

Then all you need to do is to create loggers with:

Loggerlogger=MyLogManager.Instance.GetLogger("name");

or

Loggerlogger=MyLogManager.Instance.GetCurrentClassLogger();

If you want multiple assemblies to share thisMyLogManager – just make it a public class and get others to use it.

NLog LogFactory will attempt to automatically shutdown together with the AppDomain / ProcessExit. But it is recommended to shutdown the isolate LogFactory in the plugin-dispose-stage by callingLogFactory.Shutdown().

See Also:Explicit NLog configuration loading

-Troubleshooting Guide - See available NLog Targets and Layouts:https://nlog-project.org/config

Configuration

Programmatic Configuration

Advanced

Extending NLog

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp