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

  • Thread starter Thread starter Bobby Lawrence
  • Start date Start date
B

Bobby Lawrence

Guest
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]: HPMYh.png

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.

Continue reading...
 
Back
Top