Serilog sql server sink not writing messages

0

-=-=-=-= Latest update

In my catch block, I brought up the exception variable in Quick Watch, and drilling down the inner exceptions showed me this error:

Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x86.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Problem is, in my bin\debug directory, that file is there!!! So why can it not be found? I tried but was not able to reference it directly.

-=-=-= Update

A suggested answer below had some links which I re-examined. I picked out useful code, such as assigning my log object to the static logger, followed by a call to CloseAndFlush.

That showed me Microsoft.Data.SqlClient.dll errors when the CloseAndFlush call was executed. I used NuGet to update SQLClient, but still getting those errors:

Exception thrown: 'System.DllNotFoundException' in Microsoft.Data.SqlClient.dll
Exception thrown: 'System.TypeInitializationException' in Microsoft.Data.SqlClient.dll

I am now investigating those SQL.Client errors.

-=-=-=-=-=-=

Working with Asp.NET, not core. Trying to get a basic sql server sink to work, having no luck. Something very simple must be the problem, but I can't see it.

Below is the table to write to, it already exists. With the seriuser account I can connect via SSMS, and perform all CRUD operations. I also wrote a method that uses SQL data objects that connects to the db and I can perform all CRUD operations on it that way also. But with Serilog, the log statement logs nothing. What could I be missing? The database is local on my machine. Breakpoint shows a valid logger object. Tried it also by passing in schemaname in sinkoptions, no luck. And no errors, had a try catch block around logger code previously.

A solution with a console app and a class library. Can't get much simpler than this.

The console app has this in program.cs:

    class Program
    {
        static void Main(string[] args)
        {
            SLogger logger = new SLogger();        
        }
    }

The class file and sql table:

public class SLogger
{  
    public SLogger()
    {
        var columnOption = new ColumnOptions();
        columnOption.Store.Remove(StandardColumn.MessageTemplate);
        ILogger log = new LoggerConfiguration()
           .MinimumLevel.Verbose()
           .WriteTo.MSSqlServer(@"Data Source=LW39\QA;Initial Catalog=DEV;user id=seriuser;Password=SERIUSER1;",
                                 sinkOptions: new MSSqlServerSinkOptions { TableName = "Logs" },
                                 columnOptions: columnOption)
           .CreateLogger();
        log.Information("Logger created.");  
         .....
         .....              
CREATE TABLE [dbo].[Logs](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Message] [nvarchar](max) NULL,
    [MessageTemplate] [nvarchar](max) NULL,
    [Level] [nvarchar](128) NULL,
    [TimeStamp] [datetime] NOT NULL,
    [Exception] [nvarchar](max) NULL,
    [Properties] [nvarchar](max) NULL,
 CONSTRAINT [PK_Logs] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
serilog
asked on Stack Overflow May 12, 2021 by THedge • edited May 19, 2021 by THedge

2 Answers

0

Mentioned in my last update that I was receiving this error, which I beleived was the root cause of my problem:

Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x86.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

and my confusion because said file was in the class library directory.

As I also mentioned, I had two projects in my solution: the class library and the test console app. Turns out the dll need to be in the bin directory of the console app!! Specifically in the x86 directory, which I had to create.

Once I placed the file there, the problem was solved. Thanks for all who helped!

answered on Stack Overflow May 25, 2021 by THedge
-1

This is a very common question. Lots of answers here on SO:

answered on Stack Overflow May 12, 2021 by C. Augusto Proiete

User contributions licensed under CC BY-SA 3.0