Michael Edenfield Michael Edenfield - 1 year ago 67
C# Question

How do I get MetroLog to start logging in a Windows 8.1 Store App?

I am trying to use MetroLog to write log files for a Windows Store application. I've followed the very sparse documentation (basically, copied code from the Windows 8 sample), but I'm not getting anything logged anywhere that I can tell.

I started with a basic Windows 8.1 Store app (using the "Split App" template), and in the constructor for my

class I've done this:

new FileStreamingTarget());

var logger = LogManagerFactory.DefaultLogManager.GetLogger<App>();

logger.Trace("Sample MetroLog Trace Message");
logger.Debug("Sample MetroLog Debug Message");
logger.Info("Sample MetroLog Info Message");
logger.Warn("Sample MetroLog Warn Message");
logger.Error("Sample MetroLog Error Message");
logger.Fatal("Sample MetroLog Fatal Message");

From what I can tell, I should be getting log messages in my local storage, in a folder named MetroLogs, but I don't see any data being written anywhere. The logger claims all of those log levels are enabled but nothing is written to disk. The Output window in Visual Studio does show me this:

1|2014-08-20T19:43:46.1197131+00:00|TRACE|3|Created Logger 'App'

but that is the only activity I see from MetroLog at all.

I installed MetroLog by using NuGet, but I suspect that may be part of the problem. NuGet added the following references:

<Reference Include="MetroLog">
<Reference Include="MetroLog.Platform">
<Reference Include="MetroLog.WinRT">

which looks correct. I did notice that Visual Studio complains if I try to inspect
at debug time, because it seems to be defined in two different assemblies. Should I not have both of those references in the same project?

Answer Source

For anyone else that runs into this problem:

It appears that, by default, the LogConfiguration object (including the default one) is turned off. None of the MetroLog samples seem to reflect this but in order to make any logging happen, I had manually enable it.

I changed my code to this:

var configuration = new LoggingConfiguration();
configuration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new DebugTarget());
configuration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new StreamingFileTarget());
configuration.IsEnabled = true;

LogManagerFactory.DefaultConfiguration = configuration;

And it's now working.