Sample code for 30+ languages & platforms
SQL Server

Enter/Leave Context in Logging

Demonstrates EnterContext and LeaveContext using the Chilkat Log API.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @log int
    EXEC @hr = sp_OACreate 'Chilkat.Log', @log OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Initialize the log object with an initial context tag:
    EXEC sp_OAMethod @log, 'Clear', NULL, 'myLog'

    -- Add some information..
    EXEC sp_OAMethod @log, 'LogInfo', NULL, 'Hello, I''m here...'

    -- Open a sub-context
    EXEC sp_OAMethod @log, 'EnterContext', NULL, 'abc'

    -- New information is now logged within the "abc" context.
    EXEC sp_OAMethod @log, 'LogInfo', NULL, 'This is inside the abc context'
    EXEC sp_OAMethod @log, 'LogError', NULL, 'File not found.'

    -- Perhaps open a new context...
    EXEC sp_OAMethod @log, 'EnterContext', NULL, 'fileInfo'
    EXEC sp_OAMethod @log, 'LogData', NULL, 'filename', 'something.txt'
    EXEC sp_OAMethod @log, 'LogData', NULL, 'path', '/somedir/xyz'
    EXEC sp_OAMethod @log, 'LeaveContext', NULL

    -- Close the "abc" context.
    EXEC sp_OAMethod @log, 'LeaveContext', NULL

    -- Examine the content of the log:
    EXEC sp_OAGetProperty @log, 'LastErrorText', @sTmp0 OUT
    PRINT @sTmp0

    -- This is the output:

    -- myLog:
    --   Hello, I'm here...
    --   abc:
    --     This is inside the abc context
    --     File not found.
    --     fileInfo:
    --       filename: something.txt
    --       path: /somedir/xyz
    --     --fileInfo
    --   --abc
    -- --myLog

    EXEC @hr = sp_OADestroy @log


END
GO