sorosh_sabz
sorosh_sabz

Reputation: 3003

How to use NLog in Xamarin Android

I want to use NLog in my Xamarin.Droid project. I installed NLog.Config and dependencies and move NLog.config and NLog.xsd manually to Assets folder and change NLog.config build action to AndroidAsset.

As you can see in Load automatically NLog.config from Assets folder I guess there is no problem to put NLog.config into Assets folder.

After that I change NLog.config like below

<targets>
    <target name="console" xsi:type="Console" layout="${longdate} ${callsite} ${level} ${message}"/>
</targets>
<rules>
    <logger name="*" minlevel="Info" writeTo="console" />
</rules>

After that I write some code like below for write some log in Console

ILogger logger = LogManager.GetCurrentClassLogger();
for (int c = 0; c < 1000; ++c)
    logger.Debug("Hi there");

But after that I could not see any my messages in Android Device Logging or Output tab when "Show output from" set to Debug.

Do I look to the right places?

Upvotes: 1

Views: 7038

Answers (2)

Rolf Kristensen
Rolf Kristensen

Reputation: 19847

NLog v5 no longer automatically scans for NLog.config in the Androids Assets-folder.

Either perform explicit loading of the NLog.config from the Assets-folder, or consider embedding the NLog.config as Assembly-Ressource:

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

See also: https://github.com/NLog/NLog/wiki/Explicit-NLog-configuration-loading

Notice NLog.Targets.MauiLog can also be used for platform specific output:

  • Android - Android.Util.Log / LogCat
  • Apple iOS / MacOS - Unified Logging OSLog (replacement of print and NSLog)

Upvotes: 1

Julian
Julian

Reputation: 36710

Your config says minlevel info:

<logger name="*" minlevel="Info" writeTo="console" />

but your are writing debug level. Which is below info.

logger.Debug("Hi there");

So change your logger rule to: (level name & attribute names are case insensitive)

<logger name="*" minlevel="debug" writeTo="console" />

Upvotes: 2

Related Questions