none
What are the specifics and basics of connecting Serilog to a MSSQL database? RRS feed

  • Question

  • What are the specifics and basics of connecting Serilog to a MSSQL database?  Here is what I have so far.  Can you please tell where I am going wrong?

    In the app.config I have:

            <?xml version="1.0" encoding="utf-8" ?>
        <configuration>
            <startup> 
                <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
            </startup>
          <connectionStrings>
            <add name="LogConnectionString" connectionString="Data Source=data.dev.db.com,14330;Integrated Security=SSPI;Database=DNA_Logs;"
              providerName="System.Data.SqlClient"/>
          </connectionStrings>
        </configuration>

    In the C# code I have:

        public Guid transactionGuid = Guid.NewGuid();

        public string transactionGUIDString = string.Empty;

        public StringWriter TraceLog = new StringWriter();

        public void HelloLog()
        {
            this.transactionGUIDString = transactionGuid.ToString("B").ToUpper();

            var logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .Enrich.WithProperty("TransactionId", this.transactionGuid)
                .WriteTo.MSSqlServer("LogConnectionString", "Serilog_Logs")
                .WriteTo.TextWriter(TraceLog)
                .CreateLogger();

            // Output logs
            logger.Information("Hello, Serilog!");
        }

    And the database table is configured like this:

    [![enter image description here][1]][1]


      [1]:

    So I have a few questions.  First of all, since the database table is technically named "dbo.Serilog_Logs" shouldn't the C# code use this table name instead of just "Serilog_Logs"?

    I found some sample code for Serilog but it only writes to the console.  I want to modify this sample code to write to the MSSQL Server.  I wonder how the columns of the table are supposed to be configured.
    Tuesday, March 19, 2019 4:37 PM

All replies

  • First of all, since the database table is technically named "dbo.Serilog_Logs" shouldn't the C# code use this table name instead of just "Serilog_Logs"?

    The table name is "Serilog_Logs". "dbo" is the schema in which it is located. It is a best practice to refer to objects (tables, views, stored procedures, etc.) with a 2 part name by including the appropriate schema. If you do not, the database engine will need to "figure out" which schema to find the table - which can have a slight performance impact. 

    I found some sample code for Serilog but it only writes to the console.  I want to modify this sample code to write to the MSSQL Server.  I wonder how the columns of the table are supposed to be configured.

    Go read the documentation and visit the linked "sinks". There is one for sql server; you are also free to write your own. Posting in a sql server forum is probably not the best way to get information about this package and how to use it.  

    Tuesday, March 19, 2019 5:21 PM
  • There are articles concerning Serilog qnd using MSSQL from ASP.NET preceptive.
    Wednesday, March 20, 2019 5:44 AM