我需要登录到:
我可以看到 EntLib (5.0) 有
Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener
登录到滚动日志文件。为了使日志条目看起来像一个 CSV 行,我可以更改
Formatters.TextFormatter.Template
在值周围加上双引号,并将监听器的页脚和页眉更改为空,因此它们不会被输出。在正常情况下,这会给我一个格式良好的 CSV 文件。但是,如果
Template
中的 token 值包含双引号,这不会被转义。因此,日志文件成为无效的 CSV 文件。有没有办法解决这个问题?
这个问题有什么替代解决方案吗?
最佳答案
见 http://msdn.microsoft.com/en-us/library/ff650608.aspx .
结果发现添加自定义格式化程序并不难,我添加了一个 CSVTextFormattter 来只处理消息和扩展属性,这对我有用。
请注意,我使用内置的 TextFormatter 来完成所有繁重的工作。
示例配置:
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
...
<formatters>
<add type="<your namespace>.CSVTextFormatter, <your dll>"
template="{timestamp(local)},{severity},{category},{message},{property(ActivityId)},{eventid},{win32ThreadId},{threadName},{dictionary({key} - {value}{newline})}"
name="CSV Text Formatter" />
</formatters>...
</loggingConfiguration>
这个类是这样的:
Public Class CSVTextFormatter
Implements ILogFormatter
Private Const csTemplateAttributeName As String = "template"
Private moTextFormatter As TextFormatter
Private Property TextFormatter() As TextFormatter
Get
Return moTextFormatter
End Get
Set(ByVal value As TextFormatter)
moTextFormatter = value
End Set
End Property
Private moConfigData As System.Collections.Specialized.NameValueCollection
Private Property ConfigData() As System.Collections.Specialized.NameValueCollection
Get
Return moConfigData
End Get
Set(ByVal value As System.Collections.Specialized.NameValueCollection)
moConfigData = value
If moConfigData.AllKeys.Contains(csTemplateAttributeName) Then
TextFormatter = New TextFormatter(moConfigData(csTemplateAttributeName))
Else
TextFormatter = New TextFormatter()
End If
End Set
End Property
Public Sub New()
TextFormatter = New TextFormatter()
End Sub
Public Sub New(ByVal configData As System.Collections.Specialized.NameValueCollection)
Me.ConfigData = configData
End Sub
Public Function Format(ByVal log As Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry) As String Implements Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.ILogFormatter.Format
Dim oLog As Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry = log.Clone()
With oLog
.Message = NormalizeToCSVValue(.Message)
For Each sKey In .ExtendedProperties.Keys
Dim sValue As String = TryCast(.ExtendedProperties(sKey), String)
If Not String.IsNullOrEmpty(sValue) Then
.ExtendedProperties(sKey) = NormalizeToCSVValue(sValue)
End If
Next
End With
Return TextFormatter.Format(oLog)
End Function
Private Shared Function NormalizeToCSVValue(ByVal text As String) As String
Dim bWrapLogText = False
Dim oQualifiers = New String() {""""}
For Each sQualifier In oQualifiers
If text.Contains(sQualifier) Then
text = text.Replace(sQualifier, String.Format("""{0}""", sQualifier))
bWrapLogText = True
End If
Next
Dim oDelimiters = New String() {",", vbLf, vbCr, vbCrLf}
If text.Contains(oDelimiters) Then
bWrapLogText = True
End If
If bWrapLogText Then
text = String.Format("""{0}""", text)
End If
Return text
End Function
End Class
关于.net - 使用 Enterprise Library 登录到滚动 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2785655/