hi ,
i used nlog to get log in xamarinform but the nlog not loged any thing
whats problem?
What I have tried:
nlog.config:
<pre>="1.0"="utf-8"
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false">
<targets>
<target name="logfile"
xsi:type="File"
fileName="${specialfolder:folder=LocalApplicationData}/logs/nlog.csv"
archiveFileName="${specialfolder:folder=LocalApplicationData}/logs/nlog-{#}.csv"
archiveEvery="Hour"
archiveNumbering="Date"
maxArchiveFiles="5"
archiveDateFormat="yyyy-MM-dd-HH-mm"
encoding="UTF-8">
<layout xsi:type="CSVLayout">
<quoting>All</quoting>
<withHeader>true</withHeader>
<delimiter>Comma</delimiter>
<column name="time" layout="${longdate}" />
<column name="logger" layout="${logger}"/>
<column name="level" layout="${level}"/>
<column name="machinename" layout="${machinename}"/>
<column name="windows-identity" layout="${windows-identity}"/>
<column name="appdomain" layout="${appdomain}"/>
<column name="processid" layout="${processid}"/>
<column name="processname" layout="${processname}"/>
<column name="threadid" layout="${threadid}"/>
<column name="message" layout="${message}" />
<column name="stacktrace" layout="${exception:format=Type,Message,StackTrace,Data:maxInnerExceptionLevel=5}" />
</layout>
</target>
<target xsi:type="Console" name="console" layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
<rules>
<logger rulename="logfilelogger" name="*" minlevel="Warn" writeTo="logfile" />
<logger rulename="consolelogger" name="*" minlevel="Debug" writeTo="console" />
</rules>
</nlog>
interface:
using System.Reflection;
namespace MyApp.Interfaces
{
public interface ILogService
{
void Initialize(Assembly assembly, string assemblyName);
}
}
impeliment interface:
public class LogService : ILogService
{
public void Initialize(Assembly assembly, string assemblyName)
{
string resourcePrefix;
if (Device.RuntimePlatform == Device.iOS)
resourcePrefix = "_sapmletask.iOS";
else if (Device.RuntimePlatform == Device.Android)
resourcePrefix = "_sapmletask.Droid";
else
throw new Exception("Could not initialize Logger: Unknonw Platform");
string location = $"{resourcePrefix}.NLog.config";
Stream stream = assembly.GetManifestResourceStream(location);
if (stream == null)
throw new Exception($"The resource '{location}' was not loaded properly.");
LogManager.Configuration = new XmlLoggingConfiguration(System.Xml.XmlReader.Create(stream), null);
}
}
main:
private readonly NLog.ILogger _logger = NLog.LogManager.GetCurrentClassLogger();
public MainPage()
{
InitializeComponent();
_logger.Info("im was here");
_logger.Debug("im was here");
_logger.Info("Application Start.");
}
activity:
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
InitializeNLog();
LoadApplication(new App());
}
private void InitializeNLog()
{
Assembly assembly = this.GetType().Assembly;
string assemblyName = assembly.GetName().Name;
new Helpers.LogService().Initialize(assembly, assemblyName);
}