serilog-contrib / serilog-sinks-grafana-loki

A Serilog sink sending log events to Grafana Loki
MIT License
197 stars 30 forks source link

Not sending logs to Loki service #272

Closed a11smiles closed 3 months ago

a11smiles commented 3 months ago

Which version of Serilog.Sinks.Grafana.Loki are you using?

8.3.0

Which version of .NET are you using?

net8.0

Describe the bug

I'm developing an Azure function. While it's logging to the console, it's not sending any information to the hosted Loki service.

Here's my code:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Formatting.Compact;
using Serilog.Sinks.Grafana.Loki;

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .WriteTo.GrafanaLoki(
        "https://logs-prod-us-central2.grafana.net",
        credentials: new LokiCredentials()
        {
            Login = "######",
            Password = "<redacted>"
        })
    .CreateLogger();

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices((ctx, svcs) =>
    {
        svcs.AddLogging(lb => lb.AddSerilog(Log.Logger, true));
    })
    .UseSerilog((ctx, cfg) =>
    {
        cfg.Enrich.WithProperty("Application", ctx.HostingEnvironment.ApplicationName)
            .Enrich.WithProperty("Environment", ctx.HostingEnvironment.EnvironmentName)
            .WriteTo.Console(new RenderedCompactJsonFormatter());
    })
    .Build();

try
{
    Log.Information("Starting the Host");
    host.Run();
}
catch (Exception exc)
{
    Log.Fatal(exc, "Host Terminated Unexpectedly");
}

Log.CloseAndFlush();

To Reproduce

See above code. Running Azure Function in isolated mode.

Expected behavior

Send logs to public (hosted) Loki service.

Log/SelfLog output or exception with stacktrace

No response

Application or code sample, which could be used to reproduce a bug

No response

Additional context

No response

I have read the documentation

mishamyte commented 3 months ago

Hey, I have no opportunity to reproduce in Azure Fn right now.

But on the first view it looks like you rely on a static logger, which is flushed in the end. But I expect that your host will be running and that line will be executed before the shutdown.

I would suggest adding a logger into DI in the same way as it is done in the sample web app and check will the behavior be different

a11smiles commented 3 months ago

Here's the source for a sample endpoint:

using System.Collections.Generic;
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace Fns.Logging;

public class Log
{
    private readonly ILogger _logger;

    public Log(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<Log>();
    }

    [Function("Log")]
    public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
        FunctionContext executionContext)
    {
        _logger.LogInformation("C# HTTP trigger function processed a request.");

        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

        response.WriteString("Welcome to Azure Functions!");

        return response;

    }
}

As you can see, I'm injecting the logger into the endpoint and attempting to log information to the sink.

a11smiles commented 3 months ago

Just FYI, my firewall does not show any calls being made out to grafana from my machine, not even on application shutdown (which should at least flush the stream).

a11smiles commented 3 months ago

Figured this out. Some of the documentation (not yours) was out of date. Sorry for the hassle.

github-actions[bot] commented 2 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue.