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

Explicit NLog configuration loading

Rolf Kristensen edited this pageJun 24, 2023 ·28 revisions

ℹ️ See alsoEnvironment specific NLog Logging Configuration

Loading NLog configuration from file

NLog will automaticallyscan for configuration files, but sometimes the platform requires explicit configuration loading like this:

NLog.LogManager.Configuration=newNLog.Config.XmlLoggingConfiguration("nlog.config");

Loading NLog configuration from string

The configuration can also come from standard string like this:

varxmlStream=newSystem.IO.StringReader(xmlString);varxmlReader=System.Xml.XmlReader.Create(xmlStream);NLog.LogManager.Configuration=newNLog.Config.XmlLoggingConfiguration(xmlReader,null);

NLog v5 introduces this fluent extension-method:

NLog.LogManager.Setup().LoadConfigurationFromXml(xmlString);

Loading NLog config from Assembly Resource

You need to put the NLog.config file into the application-project, then edit file's properties - change thebuild action toembedded resource.

publicstaticStreamGetEmbeddedResourceStream(Assemblyassembly,stringresourceFileName){varresourcePaths=assembly.GetManifestResourceNames().Where(x=>x.EndsWith(resourceFileName,StringComparison.OrdinalIgnoreCase)).ToList();if(resourcePaths.Count==1){returnassembly.GetManifestResourceStream(resourcePaths.Single());}returnnull;}varnlogConfigFile=GetEmbeddedResourceStream(myAssembly,"NLog.config");if(nlogConfigFile!=null){varxmlReader=System.Xml.XmlReader.Create(nlogConfigFile);NLog.LogManager.Configuration=newXmlLoggingConfiguration(xmlReader,null);}

NLog v5 introduces thisfluent configuration extension-method:

NLog.LogManager.Setup().LoadConfigurationFromAssemblyResource(typeof(App).GetTypeInfo().Assembly);

Xamarin Android

⚠️ NLog v5 will no longer scan theassets folder forNLog.config. Instead consider using Embedded Assembly Resource

With NLog v4 then the NLog.dll built for Xamarin Android would automatically scan theassets folder forNLog.config.

If the file name is different, then NLog v4 also supported this:

LogManager.Configuration=newXmlLoggingConfiguration("assets/someothername.config");

If using the NLog.dll built for NetStandard in Xamarin, then the Androidassets-folder is not recognized or scanned. Instead consider using Assembly Resource.

To explicly read file fromAndroid Assets, then one can do this:

AssetManagerassets=this.Assets;varassetStream=assets.Open("NLog.config");varxmlReader=System.Xml.XmlReader.Create(assetStream);NLog.LogManager.Configuration=newXmlLoggingConfiguration(xmlReader,null);

-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