Add data to new columns in SQL Server with Serilog

716 views Asked by At

I'm using SerlLog with SQL Server and after configuration I have the following table in SQL Server:

enter image description here

I want to add data to the columns Message LikeExpression and other columns for occurrence.

I tried the following code

Log.ForContext("Message", "Some Message")
   .ForContext("LikeExpression", ex.Message)
   .Fatal("{StackTrace}", ex.Message, ex.StackTrace);

and this adds the following data to the table

enter image description here

I must have the Some Message string in Message column but it does not work any help please?

1

There are 1 answers

0
hala tech On

Finally I found the answer: I just reomoved the column Message from the table as it consider a stundered column by using xml configuration like this

<RemoveStandardColumns>
      **<remove Name="Message"/>**
      <remove Name="MessageTemplate"/>
      <remove Name="Level"/>
      <remove Name="LogEvent"/>
      <remove Name="Exception"/>
      <remove Name="TimeStamp"/>
    </RemoveStandardColumns>

then add it again as an ordinary column in c# using

new SqlColumn
                        {ColumnName = "Message", DataType = SqlDbType.VarChar, DataLength = 1024},

then to add data log to this table using serilog I used the following

         try
            {
                int b = 0;
                int a = 1 / b;
            }
            catch (Exception ex)
            {
                int x = OccuranceCalc("SCA");
                Log.ForContext("Message", "some Message")
                    .ForContext("LikeExpression", ex.Message)
                    .ForContext("OccurrenceCount", 1)
                    .ForContext("LastOccurrence", DateTime.Now)
                    .Fatal("{StackTrace}", ex.StackTrace);
            }