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
BarnumBailey
  • 391
  • 1
  • 4
  • 13
  • Generally the log methods won't bubble up an exception if something goes wrong during logging. Most of the time you don't want a logging issue to break your app. Instead, they write to the self log. The [documentation describes how to enable the self log](https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics#selflog). I suggest you check that out and report back. – mason May 12 '21 at 19:26
  • Does this answer your question? [Serilog MSSqlServer sink not writing to table](https://stackoverflow.com/questions/37847284/serilog-mssqlserver-sink-not-writing-to-table) – C. Augusto Proiete May 12 '21 at 19:36

2 Answers2

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!

BarnumBailey
  • 391
  • 1
  • 4
  • 13
-1

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

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • If you believe this question is a duplicate of one of those, you can vote to close or leave a comment on the question. Your answer should not entirely consist of links to other questions. – mason May 12 '21 at 20:16
  • @mason Yup, I did vote to close as duplicate first thing - before I answered. The answer is to help discovery by the OP who's new to SO. – C. Augusto Proiete May 12 '21 at 20:45
  • I did come across those links previously, and looked at them again and tried picking out useful code, such as assigning my log object to the static logger, followed by a call to the 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 and Exception thrown: 'System.TypeInitializationException' in Microsoft.Data.SqlClient.dll" So pursuing that path. – BarnumBailey May 12 '21 at 21:04
  • @C.AugustoProiete You should remove this answer. Leaving your suggested dupes as a comment is sufficient to help discovery. By leaving an answer post (and being as high a reputation as you are) other users will see what you're doing and emulate your actions. Then we have to explain to them that no, they should not be posting answers that just contain links to other questions, even though they saw some users with a very respectable reputation amount doing so. Please don't set a bad example - remove this answer. – mason May 12 '21 at 21:06
  • @THedge You should update your question with the relevant error information you found. – mason May 13 '21 at 12:49