Skip to content

Dependency Injection Using Ninject, ILoogerFactory, Serilog #39

@HoomanBahreini

Description

@HoomanBahreini

Apologies, for raising an issue... this is just a question:

I want to implement logging, using Serilog in my ASp.Net MVC Framework application. Please not that it is not a core application, so LoggerFactory is not automatically being injected into the program.

This is the tutorial explaining how everythign is done in ASP.Net MVC Core:

  1. Add Serilog Nuger Packages

"Serilog": "2.2.0",
"Serilog.Extensions.Logging": "1.2.0",
"Serilog.Sinks.RollingFile": "2.0.0",
"Serilog.Sinks.File": "3.0.0"

  1. Add the following lines to the constructor of your Startup class

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.RollingFile(Path.Combine(env.ContentRootPath, "log-{Date}.txt"))
.CreateLogger();

  1. Add the following line to the configure method of your Startup class

loggerFactory.AddSerilog();

  1. Inject the logger to your services or controllers

public class Chat : IChat
{
// Instancia del logger
ILogger logger;

    // Injectamos el logger en el constructor
    public Chat(ILogger logger)
    {
        this.logger = logger;
    }

Now this explanation is for .Net Core, I don't know how I should implement it in my MVC project...

I don't know how to Inject the LoggerFacory in my startup class... I can initialize it in my Startup and Add Serilog to it, but then how can I inject the Logger into my Constructor?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions