开发者

Asynchronous logging in classic asp

开发者 https://www.devze.com 2023-03-02 20:55 出处:网络
For 开发者_Python百科my classic asp application deployed in IIS6.0, I need to implement logging errors to a text file asynchronously so that logging decouples applications from logging resources allow

For 开发者_Python百科my classic asp application deployed in IIS6.0, I need to implement logging errors to a text file asynchronously so that logging decouples applications from logging resources allowing the application to continue operating when the underlying logging infrastructure becomes unavailable for any reason. I read in one of the responses for the similiar type of questions that xmlhttp can be used. Is there any other way? Please help


What errors do you want to log?

ASP Error? Then you have to run all code with ON ERROR RESUME NEXT + Write a error handler on each site. Those e.g. you can include on all pages via SSI includes.

As classic asp is apartment threaded model only you can't really do this asynchronously! But you can write a COM+ component. This component has a method then where you deliver err.number, description, source (+ maybe Request.ServerVariables("URL")) ByVal and then quick return. Inside the component you can start a asynchronus thread to write a logfile or write errors to any database.

Don't know for sure if that is what you are looking for, but if so, that is the way you can do it.


Not exactly what you have asked, but here is an ASP classic logging solution for Chrome which gives a fairly consistent experience with javascript console.log() etc... The logging injects JS into the response and logs into the developer console (F12) Unit test and sample usage - inside Caveats: avoid in "script inside a script" situations

<%

' log from ASP lines to chrome dev console - using the same javascript syntax
' ref: https://developers.google.com/web/tools/chrome-devtools/console/api#dir

' to add this to your asp page:
' <!--#Include file ="log.asp"-->

' sample usage - see unit test at bottom

' to turn logging ON, you have those options:
'  console.active = true
'  run on localhost
'  add queryString log=1  (e.g. www.myweb.com?log=1)


class oConsole
    private isActive
    private oGroup
    private mGroupLabel
    private mGroupType
    Private Sub Class_Initialize(  )
        isActive = (Request.ServerVariables("SERVER_NAME") = "localhost") or _
                   (request.queryString("log") = "1") or _
                   session("log")
        set oGroup = nothing
    end sub

    Public Property Let active(a)
        isActive = a
        session("log") = cBool(a)
    End Property

    public property get active
      active = isActive
    end property

    private sub script(func, text)
        if not isActive then exit sub
        text = replace(text, """", "\""")
        if not oGroup is nothing then
            oGroup.add oGroup.count, func & "(""" & text & """)"
        else
            Response.Write "<script language=javascript>console." & func & "(""" & text & """)</script>" & vbCrLf
        end if
    end sub

    public sub log(Text)
        script "log", Text
    end sub

    public sub Warn(w)
        script "warn", w
    end sub

    public sub error(e)
        if e = "" then e = "Error 0x" & hex(err.number) & " " & err.description
        script "error", e
    end sub

    public sub assert(cond, message)
        if not cond then script "assert", """,""" & message
    end sub

    public sub logVar(Variable)
        log Variable & "=" & eval(Variable) 
    end sub

    public sub clear
        if not isActive then exit sub
        response.write "<script language=javascript>console.clear()</script>"  & vbCrLf
    end sub

    public sub group(label)
        set oGroup = CreateObject("Scripting.Dictionary")
        mGroupLabel = label
        mGroupType = "group"
    end sub

    public sub groupCollapsed(label)
        group label
        mGroupType = "groupCollapsed"
    end sub

    public sub groupEnd
        if isNull(oGroup) then exit sub
        Response.Write "<script language=javascript>" & vbCrLf
        response.write "console." & mGroupType & "(""" & mGroupLabel & """)" & vbCrLf
        dim X
        for each X in oGroup
            response.write "console." & oGroup.item(X) & vbCrLf 
        next
        response.write "console.groupEnd()" & vbCrLf
        response.write "</script>" & vbCrLf
        set oGroup = nothing
    end sub

end class

dim console
set console = new oConsole


sub logTest
    if not console.active then
      console.active = true
      console.clear 
      console.warn "logging activated for testing"
    else
        console.clear       
    end if
    console.log "hello "
    console.warn "warning"
    console.error "error"
    console.assert true, "should not see this"
    console.assert false, "Assertion"
    on error resume next
    f=1/0
    console.Error "" ' logs the Err object
    on error goto 0
    console.logVar "now"
    console.groupCollapsed "My collapsed group"
        console.log "L1"
        console.warn "W1"
        console.error "E1"
    console.groupEnd
    console.group "My group"
        console.log "L1"
        console.warn "W1"
        console.error "E1"
    console.groupEnd
    console.active = false
    console.error "can't see this"
    console.active = true
    console.log "should see that"
end sub


%>

Asynchronous logging in classic asp

0

精彩评论

暂无评论...
验证码 换一张
取 消