Reputation: 543
I am really struggling to get a basic custom target working with NLog on a Xamarin Android app.
I'd like to create a simple target to LogCat, in order to be able to use a tag so my debug will be easier and more 'human'
LogCatTarget.cs :
using NLog;
using NLog.Config;
using NLog.Targets;
using Android.Util;
namespace NLog.CustomTarget
{
[Target("LogCat")]
public sealed class LogCatTarget : TargetWithLayout
{
public LogCatTarget()
{
this.Tag = "XamTag";
}
//[RequiredParameter]
public string Tag { get; set; }
protected override void Write(LogEventInfo logEvent)
{
string logMessage = this.Layout.Render(logEvent);
SendTheMessageToLogCat(this.Tag, logMessage, logEvent.Level);
}
private void SendTheMessageToLogCat(string tag, string message, LogLevel level)
{
switch (level.Name.ToLower())
{
case ("trace"):
Log.Verbose(tag, message);
break;
case ("debug"):
Log.Debug(tag, message);
break;
case ("info"):
Log.Info(tag, message);
break;
case ("warn"):
Log.Warn(tag, message);
break;
case ("error"):
Log.Error(tag, message);
break;
case ("fatal"):
Log.Wtf(tag, message);
break;
}
}
}
}
in my MainApplication.cs
void RegisterNlog()
{
#if DEBUG
NLog.Common.InternalLogger.LogToConsole = true;
NLog.Common.InternalLogger.LogFile = "c:\\log.txt";
NLog.Common.InternalLogger.LogLevel = NLog.LogLevel.Trace;
//Reference custom Nlog extension for logcat
Target.Register<NLog.CustomTarget.LogCatTarget>("LogCat");
var logCatTarget = new NLog.CustomTarget.LogCatTarget();
logCatTarget.Layout = @"${message}";
logCatTarget.Tag = "coucou";
LogManager.Configuration.AddTarget("logcatname", logCatTarget);
var rule1 = new LoggingRule("*", NLog.LogLevel.Debug, logCatTarget);
LogManager.Configuration.LoggingRules.Add(rule1);
#endif
}
I also have a NLog.config with a console and a file target which are working fine. (This is why I am adding target and rule and not making a new config)
I launch my app and use a button which call all level of Android.Util.Log and those of Nlog logger
_butClear.Click += delegate
{
_txtDebug.Text = "";
_logger.Trace("logger test");
_logger.Debug("logger test");
_logger.Info("logger test");
_logger.Warn("logger test");
_logger.Error("logger test");
_logger.Fatal("logger test");
Android.Util.Log.Verbose("XXaaMMM", "logger test");
Android.Util.Log.Debug("XXaaMMM", "logger test");
Android.Util.Log.Info("XXaaMMM", "logger test");
Android.Util.Log.Warn("XXaaMMM", "logger test");
Android.Util.Log.Error("XXaaMMM", "logger test");
Android.Util.Log.Wtf("XXaaMMM", "logger test");
};
I put a break point in LogCatTarget.cs on write method, which is ovverided. But I never reach it...
Why ?!!!? cruel worlddddd ! :)
Thanks for your help.
Upvotes: 2
Views: 1593
Reputation: 19847
NLog.Targets.MauiLog has been released, and supports writing to:
Upvotes: 2
Reputation: 543
Rolf Kristensen gave me the answer in comments :
Simply add NLog.LogManager.ReconfigExistingLoggers();
at the end of my RegisterNlog()
did the trick !
This is not mentionned in the docs (https://github.com/NLog/NLog/wiki/Configuration-API), or I missed it.
Thanks Rolf !
Upvotes: 3