Imports System.IO
Imports System.Web

'**************************************************************************
' Name       :MyLog
'--------------------------------------------------------------------------
' Comment    :ログファイルのIOクラス
'            :
'--------------------------------------------------------------------------
' Design On  :2013/01/31
'        By  :NCS)T.Kikuchi
'--------------------------------------------------------------------------
' Update On  :2019/8/25
'        By  :MEC)T.Hashimoto
' Comment    :
'**************************************************************************
Public Class MyLog

    Public Shared Sub AddAccessLog(ByVal request As HttpRequest, ByVal application As HttpApplicationState)

        If request.Url.ToString.EndsWith(".css") Or request.Url.ToString.EndsWith(".js") Or
            request.Url.ToString.EndsWith(".jpg") Or request.Url.ToString.EndsWith(".JPG") Or
            request.Url.ToString.EndsWith(".png") Or request.Url.ToString.EndsWith(".PNG") Or
            request.Url.ToString.EndsWith(".htc") Or request.Url.ToString.EndsWith(".ico") Or
            request.Url.ToString.EndsWith(".jar") Then
            Return
        End If

        ' ログフォルダの作成
        If Not Directory.Exists(MyProperty.LOGフォルダパス) Then
            Directory.CreateDirectory(MyProperty.LOGフォルダパス)
        End If

        ' ログファイルパス
        Dim logPath As String = MyProperty.LOGフォルダパス + String.Format("access_{0}{1:00}{2:00}.log", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)

        ' ログメッセージをタブ区切りで作成。
        Dim sb As New StringBuilder()
        sb.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"))
        sb.Append(Chr(9))
        'sb.Append(System.Net.Dns.GetHostEntry(request.UserHostName).HostName)
        sb.Append(request.UserHostName)
        sb.Append(Chr(9))
        sb.Append(request.UserAgent)
        sb.Append(Chr(9))
        sb.Append(request.Url)
        sb.Append(Chr(9))
        sb.Append(request.UrlReferrer)

        ' ログファイルに追加書き込みする
        application.Lock()
        Using sw As StreamWriter = New StreamWriter(logPath, True)
            sw.WriteLine(sb.ToString())
        End Using
        application.UnLock()
    End Sub

    Public Shared Sub AddErrorLog(ByVal ex As Exception, ByVal application As HttpApplicationState)

        ' ログフォルダの作成
        If Not Directory.Exists(MyProperty.LOGフォルダパス) Then
            Directory.CreateDirectory(MyProperty.LOGフォルダパス)
        End If

        ' ログファイルパス
        Dim logPath As String = MyProperty.LOGフォルダパス + String.Format("error_{0}{1:00}.log", DateTime.Now.Year, DateTime.Now.Month)

        ' ログメッセージをタブ区切りで作成。
        Dim sb As New StringBuilder()
        sb.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"))
        sb.Append(Chr(9))
        'sb.Append(ex.Source)
        sb.Append("Error")
        sb.Append(Chr(9))
        sb.Append(ex.Message.Replace(System.Environment.NewLine, " "))
        sb.Append(Chr(9))
        sb.Append(ex.StackTrace.Replace(System.Environment.NewLine, " "))

        ' ログファイルに追加書き込みする
        application.Lock()
        Using sw As StreamWriter = New StreamWriter(logPath, True)
            sw.WriteLine(sb.ToString)
        End Using
        application.UnLock()
    End Sub

    Public Shared Sub AddWarningLog(ByVal message As String)

        ' ログフォルダの作成
        If Not Directory.Exists(MyProperty.LOGフォルダパス) Then
            Directory.CreateDirectory(MyProperty.LOGフォルダパス)
        End If

        ' ファイルパスの作成
        Dim logPath As String = MyProperty.LOGフォルダパス + String.Format("error_{0}{1:00}.log", DateTime.Now.Year, DateTime.Now.Month)

        ' ログメッセージをタブ区切りで作成。
        Dim sb As New StringBuilder()
        sb.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"))
        sb.Append(Chr(9))
        sb.Append("Warning")
        sb.Append(Chr(9))
        sb.Append(message.Replace(System.Environment.NewLine, " "))
        sb.Append(Chr(9))

        ' ログファイルに追加書き込みする
        Using sw As StreamWriter = New StreamWriter(logPath, True)
            sw.WriteLine(sb.ToString)
        End Using
    End Sub

    'Public Shared Sub captureLog(ByVal filename As String, ByVal application As HttpApplicationState, ByVal files() As System.IO.FileInfo)
    Public Shared Sub captureLog(ByVal application As HttpApplicationState, ByVal files() As System.IO.FileInfo)


        ' ログフォルダの作成_Hashimoto
        If Not Directory.Exists(MyProperty.LOG_Cフォルダパス) Then
            Directory.CreateDirectory(MyProperty.LOG_Cフォルダパス)
        End If

        ' ログファイルパス
        Dim logPath As String = MyProperty.LOG_Cフォルダパス + String.Format("capture_{0}{1:00}{2:00}{3:00}{4:00}.log", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute)
        ' ログメッセージをタブ区切りで作成。
        Dim sb As New StringBuilder()
        sb.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"))
        sb.Append(Chr(9))

        ' ログファイルに追加書き込みする
        'application.Lock()
        'Using sw As StreamWriter = New StreamWriter(logPath, True)
        '    sw.WriteLine(sb.ToString)
        '    Dim n As Integer
        '    For n = 0 To files.Length - 1
        '        sw.WriteLine(files(n))
        '    Next
        'End Using
        'application.UnLock()
    End Sub

End Class
