Showing posts with label logging. Show all posts
Showing posts with label logging. Show all posts

Wednesday, March 21, 2012

Script task: Dts.Logging.Log

I am trying to understand the use-case of Dts.Logging.Log.

Following BOL, I see that I can use Dts.Log to send a custom log message to my log provider. The following example code produces an event called "User:ScriptTaskLogEntry", with the message "My custom message here".

Dim dataBytes(0) As Byte

Dts.Log("My custom message here", 0, dataBytes)

I would like to be able to produce my own custom event (lets call it "MyCustomEvent"), as well as a custom log message. I thought it would be possible via Dts.Logging.Log, which requires that I specify an event name, in addition to a log entry. However, when I place the following code in the script task, I do not get any logging whatsoever.

Dim dataBytes(0) As Byte

Dts.Logging.Log("MyCustomEvent", System.Environment.MachineName, System.Environment.UserName, "Custom Log", "", "", "My custom message here", Now, Now, 0, dataBytes)

Any thoughts?

Thanks!

The script task can't log custom message type. The problem is that custom log message needs to be declared by task in advance so that it appears in the Details tab of the Logging window.

Custom task (reusable separately packaged .NET or C++ code, that appears on toolbox just like any Microsoft task) can do it, but this functionality is not available in Script task.sql

Script Task Logging

I seem to be doing something wrong Smile I have a script task as follows:

Imports System

Imports System.Data

Imports System.Math

Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

Public Sub Main()

Dim emptyBytes(0) As Byte

Dts.Log("The change in records between this load and the last load is " & CDbl(Dts.Variables("percent").Value) * 100 & "%.", 0, emptyBytes)

Dts.TaskResult = Dts.Results.Success

End Sub

End Class

I have logging turned on for the package going to a text file with just errors checked. For this step I specifically checked the box for logging on this step (instead of inheriting logging) checked the logging provider, and on details checked ScriptTaskLogEntry.

The upshot is, the step runs fine but I have no entry in the log. HELP Smile

Thanks,

LarryC

Well I guess I was wrong Smile It is logging just fine. I kept looking in the Execution Results for the message and it wouldn't show up. Is there a way to get the message in the Execution Results?

|||

Try using FireInformation (Dts.Events.FireInformation)

|||

Smokin! For what I was doing that worked really great. I had a step I wanted to fail the package and I wanted to see the reason I was failing the package. Using the FireError method accomplished both things I wanted: To be able to see the message in progress when interactively running, and to see the message in the log.

As an unexpected bonus, I can log failure messages now without resorting to special considerations with logging. My script task can now inherit package events which are just logging errors Smile Thanks a million.

The syntax I used for anyone else having a similar problem is:

Imports System

Imports System.Data

Imports System.Math

Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

Public Sub Main()

Dim emptyBytes(0) As Byte

Dim msg As String = "The change in records between this load and the last load is " + CStr(CDbl(Dts.Variables("percent").Value) * 100) + "%."

Dts.Events.FireError(0, "Fail Package", msg, "", 0)

Dts.TaskResult = Dts.Results.Failure

End Sub

End Class

LarryC