Tải bản đầy đủ - 0 (trang)
15-5. Extending SSIS Logging to an SQL Server Destination

15-5. Extending SSIS Logging to an SQL Server Destination

Tải bản đầy đủ - 0trang

Chapter 15 ■ Logging and Auditing



INSERT INTO sysssislog

(

event,

computer,

operator,

source,

sourceid,

executionid,

starttime,

endtime,

datacode,

databytes,

message,

ProcessID -- NOTE: This has been added to extend the logging capability

)



VALUES

(

@event,

@computer,

@operator,

@source,

@sourceid,

@executionid,

@starttime,

@endtime,

@datacode,

@databytes,

@message,

@ProcessID --NOTE: This has been added to extend the logging capability

)



RETURN 0 ;



GO

8.



Press F5 or click Execute to recompile the stored procedure.



How It Works

If you have saved logging data using the SSIS log provider for SQL Server, you may have noticed the stored

procedure (sp_ssis_addlogentry) and the table that it creates (sysssislog). These are not completely set in

stone, and you can make minor changes to the logging default element if you wish. You can do the following, for

instance:





Add or remove columns.







Extend the sp_ssis_addlogentry stored procedure (in SSIS 2012 and 2008, and 2008R2)

and sp_dts_addlogentry (in SSIS 2005) to add further elements to the logging.







Modify the table name and/or schema.



867

www.it-ebooks.info



Chapter 15 ■ Logging and Auditing



You can add further columns to the log table structure as you wish, and then extend the sp_ssis_addlogentry

logging procedure to enter any new data. You might wonder why or when this could be necessary, so here is a

plausible scenario. Let’s say that you have an extremely complex ETL procedure that runs daily and numbers

each daily process. Eventually, you will want to store a process identifier. You want to prepare the ground for this

now using a hard-coded identifier. This is exactly what we did in the recipe. It involved modifying the logging

table and the logging stored procedure.

I am presuming in this example that you are using the default object names created by SQL Server. There are

a few points to note:





You will have to find the table corresponding to the version of SQL Server that you

are using.







You are perfectly free to script any table changes rather than use SSMS if you prefer.



If you have altered the structure of the table to which SSIS logs data, you will have to alter the sp_ssis_

addlogentry stored procedure if you want to take advantage of your changes to the table. This is a standard

stored procedure, and consequently, modifying it is extremely simple. In the real world, you probably have a

table with a process identifier from which you wish to extract the latest process run ID and add it to the logged

data, rather than hard-coding it as I did here.

The table that SSIS creates for SQL Server logging—dbo.sysssislog—contains the fields shown

in Table 15-7.

Table 15-7.  Fields in the dbo.sysssislog Table



Field Name



Description



event



The event that triggered the logging (OnError, etc.)



computer



The computer where the process ran.



operator



The operator who ran the process.



source



The task that triggered the logging.



sourceid



The GUID of the task that caused logging to take place.



executionid



The execution GUID.



starttime



The date and time the process started.



endtime



The date and time the process ended.



datacode



The internal code.



databytes



The number of bytes processed.



message



The message sent from the event.



You can rename this table if you need to, without any untoward consequences. The only thing to watch for is

that if you change the table name, then you must remember to alter the logging query (sp_dts_addlogentry) so

that it points to the renamed table. Should you need to do this, the following is one way:

1.



Click the table named dbo.sysssislog in the database you chose to hold the logging

information.



2.



Right-click, and choose Rename.



3.



Replace the old table name with the new name.



868

www.it-ebooks.info



Chapter 15 ■ Logging and Auditing



4.



In SQL Server Management Studio, expand the Programmability ➤ Stored Procedures

node, and then expand System Stored Procedures.



5.



Right-click sp_ssis_addlogentry and select Modify.



6.



Replace the table name dbo.sysssislog with the new table name from step 3 at the

INSERT INTO statement.



7.



Press F5 or click Execute to recompile the stored procedure.



Hints, Tips, and Traps





You can also place the log table in a different schema if you wish—just remember to

indicate the correct schema in the stored procedure instead of “dbo”.







Remember that on SQL Server 2008 and 2012, the table is dbo.sysssislog, and on the

2005 version, it is dbo.sysdtslog90.







In SSIS 2005, the stored procedure is called sp_dts_addlogentry and is in the Stored

Procedures folder.







Of course, you may use T-SQL DDL to add any new columns.







Note that you are not modifying any of the built-in parameters passed to the stored

procedure—these must remain as initially defined.



15-6. Logging Information from an SSIS Script Task

Problem

You have a Script task in an SSIS package and you want to write information to the standard log providers from it.



Solution

Use the Dts.Log method to write to any of the usual log providers.

1.



As part of the Script task, enter the following code (here in Microsoft Visual Basic

2010) to log an event:

Dim nullBytes(0) As Byte

Dts.Log("My script task has done something",0, nullBytes)



2.



Include this snippet in the Try . . . Catch error trapping blocks of your script code, as in

the following example (C:\SQL2012DIRecipes\CH15\SCriptTaskLogging.vb):

Public Sub Main()

Try



'Some code here



Dim nullByte() As Byte

Dts.Log("Write to log that all has worked", 0, nullByte)





869

www.it-ebooks.info



Chapter 15 ■ Logging and Auditing



Catch ex As Exception

Dts.Log(ex.Message, 0, nullByte) 'Log the error message

End Try



Dts.TaskResult = ScriptResults.Success

End Sub



How It Works

There is a specific way of writing user-defined log information to all enabled log providers that can be called from

an SSIS Script task. You will need to do the following:





Ensure that the Script task is selected in the Container spane of the Configure SSIS

Logsdialog box.







In the Detail stab of the Configure SSIS Logs dialog box, ensure that Script Task Log Entry

is checked.



This script will now log process error information to all the log providers that have been enabled for the

package.



15-7. Logging from T-SQL to the SSIS Log Table

Problem

You have a complex SSIS package that frequently uses stored procedures, and you want to log process and error

information from both SSIS and T-SQL to a centralized table. You would prefer to use the built-in log table.



Solution

Create a stored procedure to write log data to the built-in the dbo.sysssislog table from T-SQL code and stored

procedures.

1.



Create a stored procedure to perform logging from your T-SQL procedures, using the

following code (C:\SQL2012DIRecipes\CH15\pr_SQLtoSSISLogging.Sql):

CREATE PROCEDURE dbo.pr_SQLtoSSISLogging

@event sysname,

@message NVARCHAR(2048),

@datacode int,

@starttime DATETIME,

@source NVARCHAR(1024),

@operator NVARCHAR(128)



AS



DECLARE @computer NVARCHAR(128)

DECLARE @sourceid UNIQUEIDENTIFIER

DECLARE @endtime DATETIME

DECLARE @executionid UNIQUEIDENTIFIER

DECLARE @databytes VARBINARY(MAX)





870

www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

15-5. Extending SSIS Logging to an SQL Server Destination

Tải bản đầy đủ ngay(0 tr)

×