Skip to content

Get Started

André Secco edited this page Jun 25, 2019 · 5 revisions

Installing

To get started, install the package.

Nuget

Install-Package XLogger.Adapters.Console

.NET CLI

dotnet add package XLogger.Adapters.Console

How to Use

You can use this adapter standalone, but if you are using .NET Core / ASP.NET Core consider using the extensions mentioned here, they make it easier to use XLogger in conjunction with the native Logging API.

Options

The following options are available:

Option Description Default Value
LogLevel Specifies the minimum level to log. Information level (2)
OnDemand Defines whether the adapter will work only on demand (true) or will work also integrated with the ASP.NET Core logging pipeline (false). false
OutputFormat Sets the output format of the messages.

* The variables availables are: @DateTime, @LogLevel, @Data, @Exception and NewLine.
* The NewLine variable is intended for line wrap and can be used multiple times.
* Each variable must be contained in curly braces (Example: {@LogLevel} or {NewLine}).
* Only the @DateTime variable contains additional format and should be used based on this example: {@DateTime:yyyy-MM-dd HH:mm:ss:fff}.
{@DateTime} [{@LogLevel}] {@Data} {@Exception}

Configuring

The codes below are based on usage in an ASP.NET Core project.

You have two ways to set adapter options:

1) Configuration based on the appsettings.json file

{
  "XLogger": {
    "Console": {
      "LogLevel": 4,
      "OnDemand": false,
      "OutputFormat": "{@DateTime:yyyy-MM-dd HH:mm:ss.fff} [{@LogLevel}] {@Data} {@Exception}"
    }
  }
}
using XLogger;

namespace test
{
    public class Program
    {
        public static void Main(string[] args) =>
            CreateWebHostBuilder(args).Build().Run();

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseXLogger(hub => {
                    hub.AddConsole();
                });
    }
}

2) Configuration on add the adapter to hub

using XLogger;

namespace test
{
    public class Program
    {
        public static void Main(string[] args) =>
            CreateWebHostBuilder(args).Build().Run();

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseXLogger(hub => {
                    hub.AddConsole(config =>
                    {
                        config.LogLevel = LogLevel.Error;
                        config.OnDemand = false;
                        config.OutputFormat = "{@DateTime:yyyy-MM-dd HH:mm:ss.fff} [{@LogLevel}] {@Data} {@Exception}";
                    });
                });
    }
}

Writing logs

To view some ways to write logs in ASP.NET Core, check here.

Output

image