StarRezDevTeam / Serilog.Sinks.AzureWebJobsTraceWriter

Serilog Azure WebJobs/Functions TraceWriter Sink
MIT License
19 stars 4 forks source link

AzureWebJobsTraceWriter sink does not log anything in the Azure Function environment #8

Closed jansoren closed 6 years ago

jansoren commented 6 years ago

I am trying out Serilog for my Azure function app and came across this sink. It logs beutifully when running my function locally. When I publish my function-app it does not log anything at all... ?

Any suggestions?

This is my setup using dependency injection:

    [FunctionName("GetSomething")]
    public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "something")]HttpRequestMessage req,
        [Inject(typeof(ILogger))]ILogger log) 
    {
        log.Information("Log message that I want to see in my Azure Function environment!");
    }
    public class InjectConfiguration : IExtensionConfigProvider
    {
        public void Initialize(ExtensionConfigContext context)
        {
            IServiceProvider serviceProvider = CreateServiceCollection(context).BuildServiceProvider(true);

            context
                .AddBindingRule<InjectAttribute>()
                .BindToInput<dynamic>(i => serviceProvider.GetRequiredService(i.Type));
        }

        private static ServiceCollection CreateServiceCollection(ExtensionConfigContext context)
        {
            var services = new ServiceCollection();

            services.AddSingleton<ILogger>( s => CreateLogger(context));
            ...

            return services;
        }

        private static Logger CreateLogger(ExtensionConfigContext context)
        {
            return new LoggerConfiguration().MinimumLevel.Verbose().WriteTo.TraceWriter(context.Trace).CreateLogger();
        }
    }

Host.json

{
  "tracing": {
    "consoleLevel": "verbose"
  }
}
vludax commented 6 years ago

Logging in Azure function apps needs to be explicitly enabled in Platform Features > Diagnostic Logs. Alternatively, you could add another Serilog sink, e.g. file, blob, etc

jansoren commented 6 years ago

@vludax - explicitly enabling the log level did not seem to help.

vludax commented 6 years ago

I've done some digging around this issue. Logs in Azure functions don't show up because ExtensionConfigContext.Trace is a host-level writer, which is different from the TraceWriter instance that can be received as a parameter to functions. Please see https://github.com/Azure/azure-webjobs-sdk-script/issues/820

In Platform Features > Log Streaming you should still see the missing logs, because that outputs both functions and host logs.

This suggests there is currently no good alternative to creating a Serilog.ILogger in each function when they receive their own TraceWriters. If you do come up with something, please let us know :)

jansoren commented 6 years ago

@vludax Thanks for clarifying answer