Tải bản đầy đủ - 0 (trang)
15-1. Logging Events from T-SQL

15-1. Logging Events from T-SQL

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

Chapter 15 ■ Logging and Auditing



2.



Create the stored procedure that is required to perform logging using the following

DDL (C:\SQL2012DIRecipes\CH15\pr_Logging_Simple.Sql):

CREATE PROCEDURE CarSales_Logging.log.pr_Logging_Simple

(

@Event VARCHAR(150)

,@Comments VARCHAR(MAX) = NULL

,@ErrorNo INT = NULL

,@ErrorDescription VARCHAR(MAX) = NULL

,@ErrorLineNo INT = NULL

,@ErrorSeverity INT = NULL

,@ErrorState INT = NULL

,@StartTime DATETIME

)



AS



INSERT INTO CarSales_Logging.log.EventDetail_Simple

(

Event

,StartTime

,Comments

,ErrorNo

,ErrorDescription

,ErrorLineNo

,ErrorSeverity

,ErrorState 

)



VALUES

(

@Event

,@StartTime

,@Comments

,@ErrorNo

,@ErrorDescription

,@ErrorLineNo

,@ErrorSeverity

,@ErrorState

) ;

GO



3.



You can then call this stored procedure from inside other procedures or scripts, as

shown in the following code:

BEGIN TRY

DECLARE @StartTime DATETIME = GETDATE()



-- your processing here





857

www.it-ebooks.info



Chapter 15 ■ Logging and Auditing



-- Here we log successful processing

EXECUTE Log.pr_Logging_Simple 'Load sales data', @StartTime, 'The sales data loaded successfully'

END TRY

BEGIN CATCH



-- Here we log unsuccessful processing

DECLARE @ErrorNo_TMP INT

DECLARE @ErrorDescription_TMP VARCHAR(MAX)

DECLARE @ErrorLineNo_TMP INT

DECLARE @ErrorSeverity_TMP INT

DECLARE @ErrorState_TMP INT



SELECT @ErrorNo_TMP = ERROR_NUMBER()

SELECT @ErrorDescription_TMP = ERROR_MESSAGE()

SELECT @ErrorLineNo_TMP = ERROR_LINE()

SELECT @ErrorSeverity_TMP = ERROR_SEVERITY()

SELECT @ErrorState_TMP = ERROR_STATE()



EXECUTE Log.pr_Logging_Simple

'Load sales data'

,@Comments = 'Error loading sales data'

,@StartTime = @STARTTIME

,@ErrorNo = @ErrorNo_TMP

,@ErrorDescription = @ErrorDescription_TMP

,@ErrorLineNo = @ErrorNo_TMP

,@ErrorSeverity = @ErrorSeverity_TMP

,@ErrorState = @ErrorState_TMP

END CATCH



How It Works

ETL processes can be large and complex, and consequently require large and complex logging systems. However,

before launching into an all-singing, all-dancing logging infrastructure, it is probably easiest to start with a simple

stored procedure to log the following:





Success/failure







The time of the event







Any error details



For the sake of brevity, I will refer to all these as “process status” elements.

The approach in this recipe requires a simple table structure to store the logged events and a stored

procedure to log events. The ‘sproc to log’ events can be called anywhere from inside another stored procedure,

or from an SSIS Execute SQL task. Also, it presumes that any stored procedure or T-SQL that logs results will use a

T-SQL TRY...CATCH pattern so that both successes and failures can be logged. This is explained in Recipe 15-8.

The logging routine works like this: first, each successful step in a stored procedure is logged using the

Log.pr_Logging_Simple stored procedure once it has completed. Any errors are passed to the CATCH block, which

uses the same procedure to log the failure and all available error information that is returned by the CATCH block.

No one could claim that this approach is all-embracing, but provided that you run the

Log.pr_Logging_Simple stored procedure after every major step in your stored procedures (and reset the

variable @STARTTIME before each step), you will have a simple yet reliable logging process.



858

www.it-ebooks.info



Chapter 15 ■ Logging and Auditing



Hints, Tips, and Traps





This recipe’s approach does not explicitly flag errors, but lets you query the columns

containing error data to isolate error records. Of course, you can create views over the

logging table to isolate errors, as shown in Recipe 15-19.







Remember to set a variable with the time that the process started. Instantiate it with

the time that the process (or part of a process) begins—not the time that you log the

outcome—or you will not be able to deduce the runtime by subtracting the start time

from the time the process ended and the logging took place. This moment can also be the

time an error occurred.







The call to log an event can be repeated several times in the course of a stored procedure.

It could be when the stored procedure starts or when a unit of work inside a stored

procedure has run.



15-2. Logging Data from SSIS

Problem

You have built an SSIS package and you want to add logging to it.



Solution

Configure SSIS logging from inside your SSIS package.

1.



Select Logging from the SSIS menu—or right-click the Control Flow pane and click

Logging. The Configure SSIS Logs:Logging dialog box will appear.



2.



When the logging screen is displayed, select the provider type from the list of Provider

types, and click Add for each one that you wish to use.



3.



Ensure that the checkbox for each log provider is checked.



4.



For each log provider, select or add a connection to the log destination by clicking the

pop-up in the Configuration column.



5.



In the left-hand Containers pane of the dialog box, check each container that you

wish to log events for (these are the various tasks that make up your SSIS package).

The dialog box should look something like Figure 15-1.



859

www.it-ebooks.info



Chapter 15 ■ Logging and Auditing



Figure 15-1.  Selecting log destinations

6.



Click the Details tab of the dialog box, and then the task for which you want to select

logged events.



7.



Check the Events checkbox to select all events.



8.



Click OK.



When you run your SSIS package, all the events that you selected will be logged to the providers that you

added.



How It Works

SSIS comes with advanced and complete logging facilities that allow you to track the progress and status of an

SSIS package as it executes. Indeed, you have seen SSIS logging if you have viewed the Progressor Execution

Resultstab in BIDS/SSDT—for you are seeing much of the same log information here as you will see in your

custom logging.

Using this built-in logging infrastructure requires you to answer three basic questions:





How are you going to log events?







Which steps in the SSIS package do you want to log?







What events do you want to log for each package step?



860

www.it-ebooks.info



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

15-1. Logging Events from T-SQL

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

×