none
请问My.Application.Log中GMT时间如何转换成本地时间 RRS feed

  • 问题

  • VB.NET中利用 My.Application.Log.WriteException 将系统异常写入本地Log文件,如以下代码:
    My.Application.Log.WriteException(ex, _
    TraceEventType.Critical, "Application Shut Down At " & _
    My.Computer.Clock.LocalTime.ToString)

    产生的Log文件内容为:
    "DefaultSource"***Critical***3***"算术运算导致溢出。 Application Shut Down At 2008-4-19 0:23:13"***************"2008-04-18T16:23:13.1875000Z"******


    其中 2008-4-19 0:23:13 为本地时间,是在程序中自己加进去的。但 2008-04-18T16:23:13.1875000Z 是由方法自动添加的,该时间默认为格林尼治标准时间。请问有没有什么方法,也能使该时间改为本地时间呢?

     

    注:

    app.config的配置文件:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <system.diagnostics>
    <sources>
    <!-- 本部分定义 My.Application.Log 的登录配置-->
    <source name="DefaultSource" switchName="DefaultSwitch">
    <listeners>
    <add name="Delimited" />
    </listeners>
    </source>
    </sources>
    <switches>
    <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
    <add name="Delimited"
    type="System.Diagnostics.DelimitedListTraceListener,
    System, Version=2.0.0.0,
    Culture=neutral, PublicKeyToken=b77a5c561934e089"
    initializeData="C:\Temp\sampleDelimitedFile.txt"
    delimiter="***"
    traceOutputOptions="DateTime" />
    </sharedListeners>
    </system.diagnostics>
    </configuration>

    2008年4月20日 15:39

答案

  • 继承DelimitedListTraceListener,自己定义如何写,看粗体的代码,其他代码都是通过reflector得到

    Code Snippet

    Imports System.Text
    Imports System.Globalization
    Public Class myDelimitedListTraceListener
        Inherits System.Diagnostics.DelimitedListTraceListener
        Sub New(ByVal filename As String)
            MyBase.New(filename)
        End Sub

        Public Overrides Sub TraceEvent(ByVal eventCache As TraceEventCache, ByVal source As String, ByVal eventType As TraceEventType, ByVal id As Integer, ByVal message As String)

            Me.WriteHeader(source, eventType, id)
            Me.WriteEscaped(message)
            Me.Write(Me.Delimiter)
            Me.Write(Me.Delimiter)
            Me.WriteFooter(eventCache)

        End Sub

        Private Sub WriteEscaped(ByVal message As String)
            If Not String.IsNullOrEmpty(message) Then
                Dim num As Integer
                Dim builder As New StringBuilder("""")
                Dim startIndex As Integer = 0
                Do While (num = message.IndexOf(""""c, startIndex) > 0)
                    builder.Append(message, startIndex, (num - startIndex))
                    builder.Append("""""")
                    startIndex = (num + 1)
                Loop
                builder.Append(message, startIndex, (message.Length - startIndex))
                builder.Append("""")
                Me.Write(builder.ToString)
            End If
        End Sub
        Friend Function IsEnabled(ByVal opts As TraceOptions) As Boolean
            Return ((opts And Me.TraceOutputOptions) <> TraceOptions.None)
        End Function

     

        Private Sub WriteFooter(ByVal eventCache As TraceEventCache)
            If (Not eventCache Is Nothing) Then
                If Me.IsEnabled(TraceOptions.ProcessId) Then
                    Me.Write(eventCache.ProcessId.ToString(CultureInfo.InvariantCulture))
                End If
                Me.Write(Me.Delimiter)
                If Me.IsEnabled(TraceOptions.LogicalOperationStack) Then
                    Me.WriteStackEscaped(eventCache.LogicalOperationStack)
                End If
                Me.Write(Me.Delimiter)
                If Me.IsEnabled(TraceOptions.ThreadId) Then
                    Me.WriteEscaped(eventCache.ThreadId.ToString(CultureInfo.InvariantCulture))
                End If
                Me.Write(Me.Delimiter)
                If Me.IsEnabled(TraceOptions.DateTime) Then
                    Me.WriteEscaped(eventCache.DateTime.ToString())
                End If

                Me.Write(Me.Delimiter)
                If Me.IsEnabled(TraceOptions.Timestamp) Then
                    Me.Write(eventCache.Timestamp.ToString(CultureInfo.InvariantCulture))
                End If
                Me.Write(Me.Delimiter)
                If Me.IsEnabled(TraceOptions.Callstack) Then
                    Me.WriteEscaped(eventCache.Callstack)
                End If
            Else
                Dim i As Integer
                For i = 0 To 5 - 1
                    Me.Write(Me.Delimiter)
                Next i
            End If
            Me.WriteLine("")
        End Sub


        Private Sub WriteHeader(ByVal source As String, ByVal eventType As TraceEventType, ByVal id As Integer)
            Me.WriteEscaped(source)
            Me.Write(Me.Delimiter)
            Me.Write(eventType.ToString)
            Me.Write(Me.Delimiter)
            Me.Write(id.ToString(CultureInfo.InvariantCulture))
            Me.Write(Me.Delimiter)
        End Sub

        Private Sub WriteStackEscaped(ByVal stack As Stack)
            Dim builder As New StringBuilder("""")
            Dim flag As Boolean = True
            Dim obj2 As Object
            For Each obj2 In stack
                Dim num As Integer
                If Not flag Then
                    builder.Append(", ")
                Else
                    flag = False
                End If
                Dim str As String = obj2.ToString
                Dim startIndex As Integer = 0
                Do While (num = str.IndexOf(""""c, startIndex) <> -1)
                    builder.Append(str, startIndex, (num - startIndex))
                    builder.Append("""""")
                    startIndex = (num + 1)
                Loop
                builder.Append(str, startIndex, (str.Length - startIndex))
            Next
            builder.Append("""")
            Me.Write(builder.ToString)
        End Sub

    End Class

     

     

    app.config

    Code Snippet

    <add name="Delimited"
          type="WindowsApplication1.myDelimitedListTraceListener,WindowsApplication1"

     

     

    2008年4月21日 3:55
    版主

全部回复

  • 我试了一下,没有后面那个时间,你是如何配置的?

     

    2008年4月21日 2:23
    版主
  • 继承DelimitedListTraceListener,自己定义如何写,看粗体的代码,其他代码都是通过reflector得到

    Code Snippet

    Imports System.Text
    Imports System.Globalization
    Public Class myDelimitedListTraceListener
        Inherits System.Diagnostics.DelimitedListTraceListener
        Sub New(ByVal filename As String)
            MyBase.New(filename)
        End Sub

        Public Overrides Sub TraceEvent(ByVal eventCache As TraceEventCache, ByVal source As String, ByVal eventType As TraceEventType, ByVal id As Integer, ByVal message As String)

            Me.WriteHeader(source, eventType, id)
            Me.WriteEscaped(message)
            Me.Write(Me.Delimiter)
            Me.Write(Me.Delimiter)
            Me.WriteFooter(eventCache)

        End Sub

        Private Sub WriteEscaped(ByVal message As String)
            If Not String.IsNullOrEmpty(message) Then
                Dim num As Integer
                Dim builder As New StringBuilder("""")
                Dim startIndex As Integer = 0
                Do While (num = message.IndexOf(""""c, startIndex) > 0)
                    builder.Append(message, startIndex, (num - startIndex))
                    builder.Append("""""")
                    startIndex = (num + 1)
                Loop
                builder.Append(message, startIndex, (message.Length - startIndex))
                builder.Append("""")
                Me.Write(builder.ToString)
            End If
        End Sub
        Friend Function IsEnabled(ByVal opts As TraceOptions) As Boolean
            Return ((opts And Me.TraceOutputOptions) <> TraceOptions.None)
        End Function

     

        Private Sub WriteFooter(ByVal eventCache As TraceEventCache)
            If (Not eventCache Is Nothing) Then
                If Me.IsEnabled(TraceOptions.ProcessId) Then
                    Me.Write(eventCache.ProcessId.ToString(CultureInfo.InvariantCulture))
                End If
                Me.Write(Me.Delimiter)
                If Me.IsEnabled(TraceOptions.LogicalOperationStack) Then
                    Me.WriteStackEscaped(eventCache.LogicalOperationStack)
                End If
                Me.Write(Me.Delimiter)
                If Me.IsEnabled(TraceOptions.ThreadId) Then
                    Me.WriteEscaped(eventCache.ThreadId.ToString(CultureInfo.InvariantCulture))
                End If
                Me.Write(Me.Delimiter)
                If Me.IsEnabled(TraceOptions.DateTime) Then
                    Me.WriteEscaped(eventCache.DateTime.ToString())
                End If

                Me.Write(Me.Delimiter)
                If Me.IsEnabled(TraceOptions.Timestamp) Then
                    Me.Write(eventCache.Timestamp.ToString(CultureInfo.InvariantCulture))
                End If
                Me.Write(Me.Delimiter)
                If Me.IsEnabled(TraceOptions.Callstack) Then
                    Me.WriteEscaped(eventCache.Callstack)
                End If
            Else
                Dim i As Integer
                For i = 0 To 5 - 1
                    Me.Write(Me.Delimiter)
                Next i
            End If
            Me.WriteLine("")
        End Sub


        Private Sub WriteHeader(ByVal source As String, ByVal eventType As TraceEventType, ByVal id As Integer)
            Me.WriteEscaped(source)
            Me.Write(Me.Delimiter)
            Me.Write(eventType.ToString)
            Me.Write(Me.Delimiter)
            Me.Write(id.ToString(CultureInfo.InvariantCulture))
            Me.Write(Me.Delimiter)
        End Sub

        Private Sub WriteStackEscaped(ByVal stack As Stack)
            Dim builder As New StringBuilder("""")
            Dim flag As Boolean = True
            Dim obj2 As Object
            For Each obj2 In stack
                Dim num As Integer
                If Not flag Then
                    builder.Append(", ")
                Else
                    flag = False
                End If
                Dim str As String = obj2.ToString
                Dim startIndex As Integer = 0
                Do While (num = str.IndexOf(""""c, startIndex) <> -1)
                    builder.Append(str, startIndex, (num - startIndex))
                    builder.Append("""""")
                    startIndex = (num + 1)
                Loop
                builder.Append(str, startIndex, (str.Length - startIndex))
            Next
            builder.Append("""")
            Me.Write(builder.ToString)
        End Sub

    End Class

     

     

    app.config

    Code Snippet

    <add name="Delimited"
          type="WindowsApplication1.myDelimitedListTraceListener,WindowsApplication1"

     

     

    2008年4月21日 3:55
    版主