7
7
using Serilog . Debugging ;
8
8
using Serilog . Events ;
9
9
using Serilog . Formatting ;
10
+ using Serilog . Sinks . MSSqlServer . Extensions ;
10
11
11
12
namespace Serilog . Sinks . MSSqlServer . Output
12
13
{
@@ -44,15 +45,15 @@ public KeyValuePair<string, object> GetStandardColumnNameAndValue(StandardColumn
44
45
switch ( column )
45
46
{
46
47
case StandardColumn . Message :
47
- return new KeyValuePair < string , object > ( _columnOptions . Message . ColumnName , RenderLogMessage ( logEvent ) ) ;
48
+ return new KeyValuePair < string , object > ( _columnOptions . Message . ColumnName , TruncateOutput ( logEvent . RenderMessage ( _formatProvider ) , _columnOptions . Message . DataLength ) ) ;
48
49
case StandardColumn . MessageTemplate :
49
- return new KeyValuePair < string , object > ( _columnOptions . MessageTemplate . ColumnName , TrimMessageTemplate ( logEvent ) ) ;
50
+ return new KeyValuePair < string , object > ( _columnOptions . MessageTemplate . ColumnName , TruncateOutput ( logEvent . MessageTemplate . Text , _columnOptions . MessageTemplate . DataLength ) ) ;
50
51
case StandardColumn . Level :
51
52
return new KeyValuePair < string , object > ( _columnOptions . Level . ColumnName , _columnOptions . Level . StoreAsEnum ? ( object ) logEvent . Level : logEvent . Level . ToString ( ) ) ;
52
53
case StandardColumn . TimeStamp :
53
54
return GetTimeStampStandardColumnNameAndValue ( logEvent ) ;
54
55
case StandardColumn . Exception :
55
- return new KeyValuePair < string , object > ( _columnOptions . Exception . ColumnName , logEvent . Exception ? . ToString ( ) ) ;
56
+ return new KeyValuePair < string , object > ( _columnOptions . Exception . ColumnName , TruncateOutput ( logEvent . Exception ? . ToString ( ) , _columnOptions . Exception . DataLength ) ) ;
56
57
case StandardColumn . Properties :
57
58
return new KeyValuePair < string , object > ( _columnOptions . Properties . ColumnName , ConvertPropertiesToXmlStructure ( logEvent . Properties ) ) ;
58
59
case StandardColumn . LogEvent :
@@ -62,33 +63,10 @@ public KeyValuePair<string, object> GetStandardColumnNameAndValue(StandardColumn
62
63
}
63
64
}
64
65
65
- private string RenderLogMessage ( LogEvent logEvent )
66
- {
67
- var logMessage = logEvent . RenderMessage ( _formatProvider ) ;
68
- var maxAllowedMessageLength = _columnOptions . Message . DataLength ;
69
-
70
- if ( maxAllowedMessageLength > 0 && logMessage . Length > maxAllowedMessageLength )
71
- {
72
- logMessage = logMessage . Substring ( 0 , maxAllowedMessageLength - 3 ) ;
73
- logMessage = $ "{ logMessage } ...";
74
- }
75
-
76
- return logMessage ;
77
- }
78
-
79
- private string TrimMessageTemplate ( LogEvent logEvent )
80
- {
81
- var messageTemplate = logEvent . MessageTemplate . Text ;
82
- var maxAllowedMessageTemplateLength = _columnOptions . MessageTemplate . DataLength ;
83
-
84
- if ( maxAllowedMessageTemplateLength > 0 && messageTemplate . Length > maxAllowedMessageTemplateLength )
85
- {
86
- messageTemplate = messageTemplate . Substring ( 0 , maxAllowedMessageTemplateLength - 3 ) ;
87
- messageTemplate = $ "{ messageTemplate } ...";
88
- }
89
-
90
- return messageTemplate ;
91
- }
66
+ private static string TruncateOutput ( string value , int dataLength ) =>
67
+ dataLength < 0
68
+ ? value // No need to truncate if length set to maximum
69
+ : value . Truncate ( dataLength , "..." ) ;
92
70
93
71
private KeyValuePair < string , object > GetTimeStampStandardColumnNameAndValue ( LogEvent logEvent )
94
72
{
0 commit comments