Skip to content

Logger: Getting started

Raphael Winkler edited this page May 29, 2022 · 1 revision

The Tasty.Logging library offers simple logging capabilities and the option to attach a class to the Logger, which acts as a console for the output. (Useful in UI applications with built-in consoles)

Getting started

First of all you'll need the dependency. You can either

  • Pull it from NuGet (recommended): Install-Package TastyApps.Core.Logging -Version 1.0.4
  • Pull from GitHub packages: dotnet add PROJECT package TastyApps.Core.Logging --version 1.0.4
  • Download and build this repository yourself

Enabling logging in your application is very simple to use. All you need to get started is this line of code:

Logger.Initialize();

Notice that you don't need to call Initialize() if you don't plan to supply it with any parameters. Simply calling "Logger.Default" will initialize a new logger if none exists.

By default the library saves the log file under "[EXE_PATH]/Logs/log.log", where EXE_PATH is the path to your executable. If you wish to change the file name, pass a string variable with the desired name.

Logger.Initialize("super_duper_log.log");

Also if you wish to have multiple instances of a logger, generate a new Logger object like this and save it as a variable:

public class SomeClass
{
    Logger additionalLogger;

    public SomeClass()
    {
        // The second parameter controls if the log file should generate a session id or not
        // The third parameter toggles the regeneration of the log file. 
        //     This means that each time you initialize the log file with the given name it is re-created
        additionalLogger = new Logger("log_additional.log", false, false);
    }
}

Logging lines

By now you should have your logger ready, so the only thing left to do is log some stuff! For this the Logger class exposes multiple methods for this, each with it's own use case. In the code below are some of the methods described.

public class SomeClass
{
    public SomeClass()
    {
        // Initialize primary logger
        Logger.Initalize();

        // Log a string with default type "INFO"
        // Log output: [ INF ] 24.05.2021 - 22:36:15.806: Foobar
        Logger.Default.WriteLog("Foobar");

        // Log a string with defined type "WARNING"
        // Log output: [ WRN ] 24.05.2021 - 22:36:15.806: Foobar
        Logger.Default.WriteLog("Foobar", Log.WARNING);

        // Log a formatted string with string format parameters
        // Log output: [ INF ] 24.05.2021 - 22:36:15.806: Foo: FirstParam (Bar: SecondParam); ThirdParam: True; FourthParam: 35
        Logger.Default.WriteLog("Foo: {0} (Bar: {1}); ThirdParam: {2}; FourthParam: {3}", "FirstParam", "SecondParam", true, 35);

        try
        {
            throw new IOException("Foobar");
        }
        catch (Exception ex)
        {
            // Log a string with exception
            // Log output: [ WRN ] 24.05.2021 - 22:36:15.806: [IOException] Log message - Exception thrown in method "Void .ctor()": Foobar (Class "Tasty.Samples", Line 57)
            Logger.Default.WriteLog("Log message", ex);
        {
    }
}

Next up: Configuration