lavspent / Lavspent.BrowserLogger

MIT License
8 stars 2 forks source link

Serilog / ILogger #3

Open emouawad opened 5 years ago

emouawad commented 5 years ago

This is very nice idea - was looking exactly for something like that!

How to explicitly do a log? i.e: browserLogger.Debug("This is a log message");

Serilog exposes an ILogger just the same as the built in ILogger instance of dotnetcore. I have successfully activated BrowserLogger and i see it is connected but though serilog is logging BrowserLogger isnt.

I am thinking maybe a logging middleware with explicit browserLogger logging method can solve my issue.

Long term fix is add a BrowserLogger serilog sink - are you interested in something like that? Meanwhile if there is no such method (call a log function) would it be possible to add it - thanks :D

petterlabraaten commented 5 years ago

Thanks for the feedback!

I'm not experienced with Serilog, but I had a go tonight where I refactored away/split out anything related to .Net Cores ILogger and made a BrowserLogger independent of whether you choose the "native", Serilogs or other logging frameworks.

I'll commit some code after some rework/cleaning, but for Serilog's case the use would look something like this:

        public Startup(IConfiguration configuration)
        {
            //...

            Log.Logger = new LoggerConfiguration()
                 .Enrich.FromLogContext()
                 .WriteTo.BrowserLogger()
                 .CreateLogger();
        }
        public void ConfigureServices(IServiceCollection services)
        {
        //...
            services.AddBrowserLogger();
            services.AddLogging(loggingBuilder =>
                     loggingBuilder.AddSerilog(dispose: true));
            services.Configure<BrowserLoggerOptions>(Configuration.GetSection("Logging"));
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
        //...

            app.UseWebSockets();
            app.UseBrowserLoggerSerilog();

        // This line has to be fixed, but for now...
            BrowserLoggerSink.Instance.Service = app.ApplicationServices.GetService<BrowserLoggerService>();
            app.UseMvc();
        }

Then from you code you either use the ILogger, or you can:

            Serilog.Log.Warning("This is a warning!");

Is this along the lines of what your want?

emouawad commented 5 years ago

Thanks a lot! didn't expect a fast one :heart: :heart: :heart:

It's definitely a step on the right track - would it much to ask if BrowserLogger can be configured from appsettings (https://github.com/serilog/serilog-settings-configuration) instead of: services.Configure(Configuration.GetSection("Logging")); has it's own settings for serilog supports multiple sinks therefore we need multiple configurations:

Something like:

"WriteTo": [ { "Name": "Console" }, { "Name": "BrowserLoggerOptions", "Args": { "ConsolePath": "console", "WebConsole": { "LogStreamUrl": "ws://localhost:5000/ls", "ShowClassName": false }, "LogLevel": { "Default": "Information", "System": "Information", "Microsoft": "Information" } } } ],