mattwcole / gelf-extensions-logging

GELF provider for Microsoft.Extensions.Logging
MIT License
109 stars 42 forks source link

Logs not pushed to GrayLog #8

Closed shah-keyur closed 6 years ago

shah-keyur commented 6 years ago

Hi ,

Based on the sample example for .NeCore2

I have configure GelfLogger and then injecting the Ilogger . Then using the logger.LogInformation("Log in gray log").

But its not appears in the Graylog.

On application start I am logging exactly as the sample example and its logs in gay log but not from controller.

`public void ConfigureServices(IServiceCollection services) { services.Configure(Configuration.GetSection("Graylog")) .AddLogging(loggingBuilder => loggingBuilder .AddConfiguration(Configuration.GetSection("Logging"))
.AddGelf());

using (var serviceProvider = services.BuildServiceProvider()) { UseLogger(serviceProvider.GetRequiredService<ILogger>()); } }`

private static void UseLogger(ILogger logger) { const string framework = "netcoreapp-4.0";

        logger.LogInformation("Information log from {framework}", framework);

        using (logger.BeginScope(("scope_field_1", "foo")))
        {
            logger.LogDebug("Debug log from {framework}", framework);

            using (logger.BeginScope(new Dictionary<string, object>
            {
                ["scope_field_2"] = "bar",
                ["scope_field_3"] = "baz"
            }))
            {
                logger.LogTrace("Trace log from {framework}", framework);
            }

            logger.LogError(new EventId(), new Exception("Example exception!"),
                "Error log from {framework}", framework);
        }
    }

public ValueController( ILogger logger) { this.logger = llogger;

    }

public async Task GetValues() { try {

                logger.Log(LogLevel.Information, "Get all years method is called{framework}", "2.0");
                logger.LogDebug("Get all years method is called{framework}", "2.0");
                logger.LogError("error occured");

        }
        catch (System.Exception ex)
        {
            // log exceptions
            return StatusCode((int)HttpStatusCode.InternalServerError);
        }
    }

using .net core 2.1 final previews 2

Appreciate your help.

Thanks, Keyur

shah-keyur commented 6 years ago

It would be good if you add working example to log from a controller.

Thanks,

mattwcole commented 6 years ago

You might want to check out the ASP.NET Core logging documentation referenced at the top of the readme; it shows how to use a logger from a controller as well as how to add custom providers like this one. I would follow the steps there and make sure console logging is working from your controller before adding the GELF provider.

The apps in the /samples directory are for those building console apps that are not using ASP.NET, but you could still run one and check that it sends logs to your Graylog server.

shah-keyur commented 6 years ago

Thanks,

Yeah from the sample application it is working.

Need figure it out why not working in the my application.

Thanks, -Keyur

mattwcole commented 6 years ago

It sounds like it would help if I added an example ASP.NET app. I'll try to get one up within the next few days. Hopefully you figure out your issue before then!

shah-keyur commented 6 years ago

I suspect the version the one which is working with a controller is Aspnetcore 2.0 and I am using 2.1 preview final 2

mattwcole commented 6 years ago

In theory it should still work, I'll have to look into it.

mattwcole commented 6 years ago

I have added a sample ASP.NET Core 2.0 project here in case that helps.

shah-keyur commented 6 years ago

Hi MattwCole,

I confirm that its the issue with AspnetCore2.1.

I try to reproduce it with a sample project and it doesn't work. Then I changed the AspnetCore2.0 and it worked.

Thanks, Keyur

mattwcole commented 6 years ago

Thanks for confirming this. Now that we know what the problem is, I will close this issue. Support for 2.1 will be tracked in #9.

shah-keyur commented 6 years ago

Appreciate your help!