GithubHelp home page GithubHelp logo

alsami / serilog.extensions.autofac.dependencyinjection Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 1.0 56 KB

Autofac plug-in for Serilog.

License: MIT License

C# 85.41% Shell 14.59%
ilogger serilog serilog-extension iloggerprovider autofac

serilog.extensions.autofac.dependencyinjection's Introduction

Serilog.Extensions.Autofac.DependencyInjection

Build Application codecov

NuGet NuGet

This is a cross platform library, written in .netstandard 2.0, that serves as an extension for autofac's containerbuilder. It will register all necessary classes and interfaces of Serilog using Microsoft's logging extensions

Installation

This package is available via nuget. You can install it using Visual-Studio-Nuget-Browser or by using the dotnet-cli

dotnet add package Serilog.Extensions.Autofac.DependencyInjection

If you want to add a specific version of this package

dotnet add package Serilog.Extensions.Autofac.DependencyInjection --version 1.0.0

For more information please visit the official dotnet-cli documentation.

Usage

For more information about the usage, please have a look at the samples.

serilog.extensions.autofac.dependencyinjection's People

Contributors

alsami avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

artimarius

serilog.extensions.autofac.dependencyinjection's Issues

Deprecated dependency

The current version depends on Serilog.Sinks.RollingFile

This has been Deprecated in favour of Serilog.Sinks.File

Logging not set up if container initialization fails

I have a singleton in my container. The registration process looks like this:

            var containerBuilder = new ContainerBuilder();

            var loggerConfiguration = new LoggerConfiguration()
                                                      .WriteTo.Console()
                                                      .ReadFrom.AppSettings();

            containerBuilder.RegisterSerilog(loggerConfiguration);

            containerBuilder.RegisterInstance(CreateSingleton());

            // [... more registration ...]

            IContainer container = containerBuilder.Build();

The problem is that if the CreateSingleton() call throws an exception, logging won't be set up yet as the SerilogModule hasn't been loaded yet.

But if I manually create the logger (Log.Logger = loggerConfiguration.CreateLogger();) before building the container and CreateSingleton() doesn't fail, then I get an exception telling me that CreateLogger() has been called twice (once explicitly by my code, and another time by the SerilogModule), which is not allowed.

This would be easy to solve by simply moving the CreateLogger() call outside the SerilogModule, i.e. directly into the RegisterSerilog method.

Is this something you would consider?

At the moment, I have to resort to this ugly workaround:

try
{
    containerBuilder.RegisterInstance(CreateSingleton());
}
catch
{
    Log.Logger = loggerConfiguration.CreateLogger();
    throw;
}

Thanks.

Not working through Module

Module registration process

public class BitMonoApplication : IApplication
{
    public AutofacServiceProvider Build()
    {
        return Build(new BitMonoAutofacServicesRegistryModule());
    }
    public AutofacServiceProvider Build(IModule module)
    {
        var containerBuilder = new ContainerBuilder();
        containerBuilder.RegisterModule(module);
        var container = containerBuilder.Build();
        return new AutofacServiceProvider(container.Resolve<ILifetimeScope>());
    }
}
public class BitMonoAutofacServicesRegistryModule : Module
{
    protected override void Load(ContainerBuilder containerBuilder)
    {
       var currentAssemblyDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
       var file = Path.Combine(currentAssemblyDirectory, "logs", "bitMono-{Date}.log");

       containerBuilder.RegisterSerilog(new LoggerConfiguration()
            .WriteTo.Console()
            .WriteTo.RollingFile(file, shared: true));
    }
}

Resolving Logger

var container = new BitMonoApplication().Build();
var logger = container.LifetimeScope.Resolve<ILogger>();

Error:
image

Working fine in this case

containerBuilder.Register<ILogger>((c, p) =>
{
    return new LoggerConfiguration()
        .WriteTo.Console()
        .WriteTo.RollingFile(file, shared: true)
        .CreateLogger();
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.