Search code examples
c#windowslogginguwpmetrolog

Using MetroLog in C# to direct the logs into a specified File Location


Similar to java.util.logging package, I am trying to use MetroLog in C# implementation of Logging. From the documentation of MetroLog, I am unable to find an example for the same.

My snippet

var naming = new FileNamingParameters()
                    {
                        IncludeLevel = false,
                        IncludeLogger = false,
                        IncludeSequence = false,
                        IncludeSession = false,
                        IncludeTimestamp = FileTimestampMode.None
                    };

var info = GetLogEventInfo();
var filename = naming.GetFilename(new LogWriteContext(), info);

Is there any way I can set my filename programmatically ?

The documentation shows the way to use the Default logging location.


Solution

  • You cannot dynamically set the File name using Metro logs, the only thing you can do is set FileNamingParameters.FileTimestampMode to either Date (for creating a file for each day) or DateTime for the filename containing Date and time both.

    If you want your own file names then use the open sourced version of the library and then change the logic for naming the file in the following method - GetOrCreateStreamWriterForFile in the FileTargetBase.cs