serilog-mssql / serilog-sinks-mssqlserver

A Serilog sink that writes events to Microsoft SQL Server and Azure SQL
Apache License 2.0
283 stars 148 forks source link

Not Working on SQL Azure #600

Closed stevanuz closed 1 hour ago

stevanuz commented 19 hours ago

Target framework: .NET 8 OS: Windows server latest

Serilog packages: Serilog.AspNetCore 8.0.3 Serilog.Sinks.MSSqlServer 8.0.0

Azure database: Basic tier

The issue is very basic, there is no exception but it does not log on SQL Azure but it does on my local machine. Initially the AutoCreateSqlTable is set to true but it didn't work on Azure, so I created the logs table manually using EF migrations. But even with that, nothing is being logged when the app started as in configuration the Serilog MinimumLevel is set to Information.

Here is the configuration code:


 public static class Configuration
 {
     public static IHostBuilder ConfigureSerilog(this IHostBuilder builder,
         IConfiguration configuration,
         IHostEnvironment environment)
     {
         if (environment.IsDevelopment())
         {
             Serilog.Debugging.SelfLog.Enable(message => Console.WriteLine(message));
         }

         var connectionString = configuration.GetConnectionString("DefaultConnection");

         var sqlOptions = new MSSqlServerSinkOptions
         {
             TableName = "Logs",
             SchemaName = "dbo"
         };

         var columnOptions = new ColumnOptions();
         columnOptions.TimeStamp.ColumnName = "TimestampUtc";
         columnOptions.TimeStamp.ConvertToUtc = true;

         return builder
             //.ConfigureLogging((context, logging) => logging.ClearProviders())
             .UseSerilog((context, logging) =>
             {
                 logging
                     .ReadFrom.Configuration(configuration)
                     .WriteTo.Console()
                     .WriteTo.MSSqlServer(connectionString, 
                         sqlOptions, 
                         columnOptions: columnOptions);
             });
     }
 }

This is how I called in Program.cs

var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureSerilog(builder.Configuration, builder.Environment);

Here is configuration appSettings:

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft.AspNetCore": "Warning"
      }
    }
  },

Can someone please point out what might be wrong or the issue is? Many thanks.

stevanus

ckadluba commented 18 hours ago

Hi @stevanuz!

The code looks correct, so it might be an issue with the connection string or with permissions on your Azure SQL instance.

Can you connect to the DB using the same connection string but with SQL Server Management Studio or some other program on your dev machine?

In the code you have enabled Serilogs SelfLog feature using the Console sink. If the MSSQL sink encounters a problem when trying to write log data to your Azure SQL DB, an error message should be written to the console. Do you see anything there?

stevanuz commented 18 hours ago

Hi @ckadluba ,

Thanks for the reply. There is only one connection string on Azure that is used by Serilog and EF DbContext and it works fine for the DbContext. I can connect fine as well from SSMS. I am not sure how can I actually see the console log as it is deployed on Azure Webapp service. I have not tried to use Azure database connection string and run it on my local machine nevertheless. I will give it a try and let you know.

stevanuz

ckadluba commented 14 hours ago

The easiest way to access the console logs in Azure App Service is to enable log streaming in the portal (App Service logs > Application Logging (Filesystem) = On).

You can then access the logs via the Azure CLI, the Kudu Debug console.

https://learn.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#enable-application-logging-windows

stevanuz commented 1 hour ago

Ok, I tested from my local machine and changed the connection string using SQL Azure and it worked but it is still not working from the Azure Webapp :(

I will try to use file log as you suggested. Thanks.

stevanuz commented 1 hour ago

Ok, it was a connection string issue as the one stored in Azure environment variable was pointing to a wrong database :( Thank you for your help :)