Nlog: Xamarin UWP doesn't create a logfile

Created on 28 Mar 2018  路  19Comments  路  Source: NLog/NLog

Type (choose one):

  • Bug

NLog version: 4.5.0-rc07

Platform: .Net 4.7

Current NLog config

<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"
      internalLogLevel="Trace" internalLogFile="C:\Users\<Path>\nlog-internal.log">
    <targets>
        <target name="file" xsi:type="File"
                layout="${date} | ${level} | ${message}"             
                fileName="${specialfolder:folder=ApplicationData}/qkiss_app-${shortdate}.log"
                keepFileOpen="false"
                encoding="utf-8" />
    </targets>
    <rules>
        <logger name="*" minlevel="Info" writeTo="file" />
    </rules>
</nlog>

No log file gets created, even when I use the absolute path for my machine: C:/Users/\/AppData/Local/Packages/\/LocalState/\-${shortdate}.log

What is the current result?

  • The logger works fine in debug mode (UWP App)
  • Log file won't be created under ReleaseMode. -> .Net Native compilation is on and may couse some
    errors (Reflection etc.)
  • ${environment:USERPROFILE} won't work either

What is the expected result?

  • Creating the log file under ReleaseMode like under DebugMode

Did you checked the Internal log?

  • It wasn't created. I did set internalLogLevel=Trace and path for internalLogFile="C:\Users...\log.log"

Please post full exception details (message, stacktrace, inner exceptions)

  • I don't get an exception. The log file never gets created

Are there any workarounds?

  • None that I know of

Is there a version in which it did work?

  • I don't it had ever worked with UWP ReleaseMode

Environment:

  • Visual Studio 2017 Version 15.6.4
  • .Net Versione 4.7.0.2556
  • NetStandard 2.0.1
  • NLOG 4.5.0-rc07
Xamarin question

All 19 comments

Did you checked the Internal log?

It wasn't created.

Or NLog can't find the nlog.config, our you haven't correct access.

Could you please test with a config made in c#?

I have outsources the path to a variable and used it for fileName. That works for debug mode but I can't use it with .Net Native compilation.
LogManager.Configuration.Variables["LogPath"] = \;

I've also tried it for internalLog but that didn't work:
internalLogLevel="Trace" internalLogFile="${var:LogPath}/nlog-internal.log"
I will try to create a NLog-Config in C#

@MaxFe Instead of using NLog-variables for changing global parameters at runtime, then I recommend that you use GDC: https://github.com/NLog/NLog/wiki/Gdc-layout-renderer

GDC parameters can be updated like this:

NLog.GlobalDiagnosticsContext.Set("LogPath", readpathForPlatform);

In the nlog.config replace ${var:LogPath} with ${gdc:item=LogPath}.

Regarding the internalLogFile, then you cannot use layout-renderers (To keep it simple and ensure it always works). I recommend you always configure the internalLogFile from code, and just have the internalLogLevel in the nlog.config.

NLog.InternalLogger.LogFile = Path.Combine(readpathForPlatform, "nlog-internal.log");

Consider loading the NLog.config manually with an explicit path. If you are being recompiled to dotnet-native, then the new binaries might be a different place in release-mode and cannot find NLog.config.

NLog.LogManager.LoadConfiguration(Path.Combine(readpathForPlatform, "nlog.config"));

It isn't working with release mode and C# approach. The error message say "System.PlatformNotSupportedException: 'Operation is not supported on this platform.'". -> I'm building for UWP.

@snakefoot Thanks for the "GDC parameters" approach. I will try that out

@MaxFe Do you have a callstack? Have you upgraded to NLog 4.5.0 RTM ? Have you made sure NOT to use autoReload="true" in the nlog.config ?

I just updated to 4.5.1 (4h ago) and autoReload was set to true. The GDC Parameter isn't throwing an exception but it isn't working. I have tried autoReload with true and false and can't see the difference. The log does not get created.

I still have the impression that .Net Native compilation is the problem.

@MaxFe And the callstack for the PlatformNotSupportedException ? (Just call ToString() on the caught exception)

When I tried the C# approach and got the "System.PlatformNotSupportedException" -> The Exception was thrown by the line

var logfile = new NLog.Targets.FileTarget {
                Name = "file",
                Layout = "${date} | ${level} | ${message}",
                FileName = logPath + "/qkiss_app-${shortdate}.log",
                KeepFileOpen = false,
                Encoding = Encoding.UTF8
            };

All that works fine with debug mode. Why is that?

@MaxFe Why is that?

That is the reason why I'm asking you to provide the stacktrace of the thrown PlatformNotSupportedException (For the third time). Again Just call ToString() on the caught exception.

StackTrace for the C# approach:

   at System.Reflection.Runtime.Assemblies.RuntimeAssembly.get_CodeBase() in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Reflection.Core\src\System\Reflection\Runtime\General\ThunkedApis.cs:line 63
   at NLog.Config.ConfigurationItemFactory.GetAssemblyFileLocation(Assembly assembly)
   at NLog.Config.ConfigurationItemFactory.BuildDefaultFactory()
   at NLog.Config.ConfigurationItemFactory.get_Default()
   at NLog.Layouts.Layout.FromString(String layoutText)
   at NLog.Layouts.Layout.op_Implicit(String text)
   at NLog.Targets.TargetWithLayout..ctor()
   at NLog.Targets.TargetWithLayoutHeaderAndFooter..ctor()
   at NLog.Targets.FileTarget..ctor()
   at Projectname.Logger.NLogManager.InitLogger() in C:\<Project>\src\Projectname.Bootstrapper\Logger\NLogManager.cs:line 14
   at Projectname.App.InitLogger() in C:\Project\src\Projectname\App.xaml.cs:line 64
   at Projectname.App..ctor(IPlatformInitializer initializer) in C:\Project\src\Projectname\App.xaml.cs:line 30
   at Project.App..ctor() in C:\<projectPath>\App.xaml.cs:line 26
   at Project.UWP.MainPage..ctor() in C:\<projectPath>\ProjectName.UWP\MainPage.xaml.cs:line 20
   at Project.UWP.Project_UWP_XamlTypeInfo.XamlTypeInfoProvider.Activate_4_MainPage() in C:\<projectPath>\ProjectName.UWP\obj\x86\Release\XamlTypeInfo.g.cs:line 293
   at ProjectName.UWP.Projectname_UWP_XamlTypeInfo.Activator.Invoke()
   at ProjectName.UWP.ProjectName_UWP_XamlTypeInfo.XamlUserType.ActivateInstance() in C:\<Project>\src\platform\ProjectName.UWP\obj\x86\Release\XamlTypeInfo.g.cs:line 713
   at __Interop.Intrinsics.HasThisCall__47(Object __this, IntPtr pfn)
   at __Interop.ReverseComStubs.Stub_19(Object __this, Void** unsafe_returnValue__retval, IntPtr __methodPtr)

Source: System.Private.Reflection.Core
Message: Operation is not supported on this platform.

StackTrace for the C# approach:

You are for some reason not using the NetStandard1.3 DLL, which works with UWP. Please check the file-properties of the NLog.dll to ensure it is says:

"NLog for NetStandard 1.3"

Think you need to figure out why your build-process is choosing the wrong NLog.dll

I'm using Xamarin.Forms 2.5.0.280555 with NetStandard 2.0.1 and NLog 4.5.1. How can I look inside the Nlog lib inside UWP? Properties on Nlog sho an empty list. I can open it with the "Nuget Package Explorer":
image

What operation system version is being used on the device ? Does it support UWP NetStandard2.0 ?

It's Windows 10 Version 1709 (Build 16299.334)

@MaxFe I see the problem now. Looks like this is not always supported:

https://github.com/dotnet/corert/blob/master/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/ThunkedApis.cs#L63

Will make a PR that fixes this.

@MaxFe Can you check if the problem is resolved if you use this NLog-nuget-package:

https://ci.appveyor.com/project/nlog/nlog/build/4.5.0-beta7399/artifacts

Visual Studio allows you to setup a "Local Package Source", where you just point to folder on you local machine that contains the downloaded NLog-nuget-package. You probably have to clean your local nuget-cache before it will use the downloaded NLog-nuget-package (Visual Studio -> Tools -> Options -> Nuget -> Clear All Nuget Caches)

@MaxFe Was able to get an UWP App running with NetStandard2.0. Discovered that callsite and GetCurrentClassLogger didn't like the new optimized Net Native.

Have updated the PR with additional fixes. Please try this instead:

https://ci.appveyor.com/project/nlog/nlog/build/4.5.0-beta7414/artifacts

I've tried https://ci.appveyor.com/project/nlog/nlog/build/4.5.0-beta7414/artifacts and
it worked pretty well. Thank you very much :-). Will it be included in the next nupkg?

@MaxFe Excellent. I guess @304NotModified will include it in NLog ver. 4.5.2 unless I have broken some sacred rules in my PR #2643

Was this page helpful?
0 / 5 - 0 ratings

Related issues

imanushin picture imanushin  路  3Comments

JustArchi picture JustArchi  路  3Comments

haythamabutair picture haythamabutair  路  3Comments

vasumsit picture vasumsit  路  3Comments

BobSeu picture BobSeu  路  3Comments