Skip to content

Get Started

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

Installing

To get started, you must install it along with one of the available adapters.

Nuget

Install-Package XLogger.AspNetCore
Install-Package XLogger.Adapters.Console

.NET CLI

dotnet add package XLogger.AspNetCore
dotnet add package XLogger.Adapters.Console

How to Use

You can register the XLogger in two ways.

Program.cs

In the CreateWebHostBuilder method in the Program class.

Use the UseXLogger() extension method and be sure to pass as a parameter which adapters you want to use.

Using only one adapter:

using XLogger;
...

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(options => options.AddConsole());
}

Using two adapters (In this example you will need to install the XLogger.Adapters.MongoDB package):

using XLogger;
...

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(options => 
	    {
	        options.AddConsole();
		options.AddMongoDB();
	    });
}

Startup.cs

In the ConfigureServices method in the Startup class.

Use the AddXLogger() extension method and be sure to pass as a parameter which adapters you want to use.

Using only one adapter:

using XLogger;
...

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddXLogger(options => options.AddConsole());
    }
}

Using two adapters (In this example you will need to install the XLogger.Adapters.MongoDB package):

using XLogger;
...

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
	services.AddXLogger(options =>
	{ 
	    options.AddConsole();
	    options.AddMongoDB();
	});
    }
}

In both ways, XLogger structures will be injected and can be retrieved by dependency injection.

XLogger will be automatically added to the ASP.NET Core logging pipeline, logging events based on the default options for each adapter.

Adapter Options

Each adapter has its own options that can be configured individually.

Default options for all adapters:

Option Description
LogLevel Specifies the minimum level to log. Default is Information level.
OnDemand Defines whether the adapter will work only on demand (true) or will work also integrated with the ASP.NET Core logging pipeline (false). Default is false.

For more details on the options for each adapter, visit the adapter repository wiki. To see the complete list of adapters, click here.

Writing logs with LoggerHub

When using LoggerHub, you will be writing simultaneously to all adapters that have been registered. To do this, use the ILoggerHub interface.

using XLogger;
...

public class HomeController : Controller
{
    private readonly ILoggerHub _logger;

    public HomeController(ILoggerHub logger) =>
        _logger = logger;

    public IActionResult Index()
    {
        _logger.Information("Hello World!");
        return View();
    }
}

Writing logs with specific adapter

For example, if you want to write to the Console adapter only, use the IConsoleLogger interface. For the MongoDB adapter, use the IMongoDBLogger interface, and so on.

Console Adapter

using XLogger.Adapters.Console;
...

public class HomeController : Controller
{
    private readonly IConsoleLogger _consoleLogger;

    public HomeController(IConsoleLogger consoleLogger) =>
        _consoleLogger = consoleLogger;

    public IActionResult Index()
    {
        _consoleLogger.Information("Hello World!");
        return View();
    }
}

MongoDB Adapter

using XLogger.Adapters.MongoDB;
...

public class HomeController : Controller
{
    private readonly IMongoDBLogger _mongoLogger;

    public HomeController(IMongoDBLogger mongoLogger) =>
        _mongoLogger = mongoLogger;

    public IActionResult Index()
    {
        _mongoLogger.Information("Hello World!");
        return View();
    }
}

Clone this wiki locally