Skip to content

PavelGriza/windows-universal-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 

Repository files navigation

WindowsUniversalLogger

The simple and light-weight logging tool for Windows Universal Applications. It inherits some principals which are presented in Windows.Foundation.Diagnostics namespace. In development we used ExGrip.WinRT.Logging solution because of interesting ideas.


Dependencies

  • xUnit - unit test framework

How it works

First of all you need to have ILoggingSession instance. You may implement your own session object or use WindowsUniversalLogger.Logging.Sessions.LoggingSession as a singleton:

ILoggingSession session = LoggingSession.Instance;

Then you need to instantiate one or several ILoggingChannel objects:

ILoggingChannel channel = new FileLoggingChannel(
    "UniqueChannelName",
    ApplicationData.Current.LocalFolder, 
    "logs.txt");
await channel.Init();
session.AddLoggingChannel(channel);

For writing logging message you need to create ILoggingEntry instance:

await LoggingSession.Instance.LogToAllChannels(
	new LogEntry(
		LogLevel.INFO,
		"App is initialized"));

How to logging unhandled exception in Windows Universal Application

For logging unhandled exceptions you need to open App.xaml.cs and add envent handler for UnhandledException event. Before using in Shared library you need to add reference to WindowsUniversalLogger.Logging library (or install from nuget package) into both of your Win and WP projects Here it's a simple code snippet:

/// ...
using WindowsUniversalLogger.Interfaces;
using WindowsUniversalLogger.Interfaces.Channels;
using WindowsUniversalLogger.Logging;
using WindowsUniversalLogger.Logging.Channels;
using WindowsUniversalLogger.Logging.Sessions;

public sealed partial class App : Application
{
	public App()
	{
		this.InitializeComponent();
		this.UnhandledException += OnApplicationUnhandledException;

		// ...
	}
	
	protected async override void OnLaunched(LaunchActivatedEventArgs e)
	{
		ILoggingSession session = LoggingSession.Instance;
		ILoggingChannel channel = new FileLoggingChannel(
			"UniqueChannelName", 
			ApplicationData.Current.LocalFolder, 
			"logs.txt");
		await channel.Init();
		session.AddLoggingChannel(channel);

		await LoggingSession.Instance.LogToAllChannels(
			new LogEntry(
				LogLevel.INFO,
				"App is initialized"));
		
		// ...
	}

	private void OnApplicationUnhandledException(object sender, UnhandledExceptionEventArgs e)
	{
		LoggingSession.Instance.LogToAllChannels(
			new LogEntry(
				LogLevel.ERROR,
				"Exception: {0}", e.Exception));

		e.Handled = true;
	}

	// ...
}

How to run unit tests

Read how to run tests with the xUnit.net console runner

Install from NuGet

Watch at Windows Universal App Logger

License

WindowsUniversalLogger is licensed under License Apache 2.0 License. Refer to license file for more information.

About

Logger for Windows Universal Application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages