<%@ Application Language="VB" %>

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application shutdown
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs

        Dim ex As Exception = Server.GetLastError()

        If TypeOf ex Is System.Web.HttpRequestValidationException Then
            Context.Server.ClearError()
            Response.Clear()
            Response.StatusCode = 200
            'Response.Write("<html><head></head><body>input error.</body></html>")
            Response.Write(
                "<html><head><title>HTML Not Allowed</title></head>" & _
                "<body style='font-family: Arial, Sans-serif;'>" & _
                "<h1>Input Error.</h1>" & _
                "<p>HTML entry is not allowed on that page.</p>" & _
                "<p>Please make sure that your entries do not contain " & _
                "any angle brackets like &lt; or &gt;.</p>" & _
                "<p><a href='javascript:history.back();'>Go back</a></p>" & _
                "</body></html>"
            )
            Response.End()
            Exit Sub
        End If

        '2014/10/06 H.Sato ADD Start >>>
        ' ログ・ファイルerr.logへの出力ストリームを生成
        Dim strTmpDir As String = ConfigurationManager.AppSettings("LogDir")
        Dim objSw As New IO.StreamWriter(strTmpDir & "\ApplicationError.log", True, Encoding.GetEncoding("Shift_JIS"))

        '2015/10/04 H.SAto MOD Start >>>
        'Dim err As String = "---------------------------------------" & _
        '            System.Environment.NewLine & _
        '            "Error Caught in Application_Error event" & _
        '            System.Environment.NewLine & _
        '            "Date:" & DateTime.Now.ToString() & _
        '            System.Environment.NewLine & _
        '            "Error in: " & Request.Url.ToString() & _
        '            System.Environment.NewLine & _
        '            "Error Message: " & ex.Message.ToString() & _
        '            System.Environment.NewLine & _
        '            "Stack Trace:" & ex.StackTrace.ToString()

        Dim message As StringBuilder = New StringBuilder()
        Dim err As String = ""
        Dim usr As String = ""
        If (Server IsNot Nothing) Then
            ' 例外の内容を取得し、StringBuilder に格納する
            While ex IsNot Nothing
                message.AppendFormat("{0}: {1}{2}", ex.GetType().FullName, ex.Message, ex.StackTrace)
                ex = ex.InnerException
            End While

            If Not (Session(AppVariable.S_COM_PortalID) Is Nothing) Then
                usr = Session(AppVariable.S_COM_PortalID).ToString
            End If

            err = "------------------------" & _
                    System.Environment.NewLine & _
                    "Date:" & DateTime.Now.ToString() & _
                    System.Environment.NewLine & _
                    "USer:[" & usr & "]" & _
                    System.Environment.NewLine & _
                    "Error in: " & Request.Url.ToString() & _
                    System.Environment.NewLine & _
                    message.ToString & _
                    System.Environment.NewLine

            ' 例外をクリア（カスタムエラーページを設定している場合はコメントアウトすること）
            'Server.ClearError()
        End If
        '2015/10/04 H.SAto MOD End <<<

        objSw.WriteLine(err)
        objSw.Close()

        ' 日付、ソース・コード、エラー・メッセージ、エラー原因となった
        ' メソッドをタブ区切りテキストで記録
        'Dim objBld As New StringBuilder()
        'objBld.Append(DateTime.Now.ToString())
        'objBld.Append(Chr(9))
        'objBld.Append(ex.Source)
        'objBld.Append(Chr(9))
        'objBld.Append(ex.Message)
        'objBld.Append(Chr(9))
        'objBld.Append(ex.TargetSite.ToString())
        'objSw.WriteLine(objBld.ToString())
        'objSw.Close()
        '2014/10/06 H.Sato ADD End <<<
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a new session is started
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a session ends. 
        ' Note: The Session_End event is raised only when the sessionstate mode
        ' is set to InProc in the Web.config file. If session mode is set to StateServer 
        ' or SQLServer, the event is not raised.
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim enableRedirect As Boolean = False

        Try
            enableRedirect = Boolean.Parse(ConfigurationManager.AppSettings("EnableComingSoonRedirect"))
        Catch
            '
        End Try

        If enableRedirect Then
            Dim currentPage As String = Request.Url.AbsolutePath.ToLower()
            If Not currentPage.EndsWith("/comingsoon.aspx") Then
                Response.Redirect("~/comingsoon.aspx")
            End If
        End If
    End Sub

</script>