SSIS Template to create Log file if not Exists and Delete Log file after Redention period

Here i want to explain you how to create a Log file if it is not exists and Delete Logfile after redention Period .

There are 2 major steps in this Template

1) Create Log file If not  with the Name <<SSIS Package name>>_YYYY-MM.log
2)Delete The Log file which is Older that Redention Period

To achive this we need 3 Parameters

1.SSIS_ErrorLog_Folder :  To indicate the Log file folder path
2.Log fileName :  To store the Logfile name . Here we want to craate File name which inculdes Cureent Year month  . we will keep log file on monthly basis and we have redention period of 2 months

 Expression to Createa logfile is

@[User::SSIS_ErrorLog_Folder] + @[System::PackageName]+ "_" +
(DT_WSTR,4)YEAR(GETDATE()) + "-" + RIGHT("0" + (DT_WSTR,2)MONTH(GETDATE()), 2)+ ".log"

The result will look like

\\SurendraThota\Dev\Logs\Package_2014-03.log



3.DeleteLogFileName : This variable is get the Logfile name of  last 2 months back  which will be used in deleteing that file from the log file folder .  This will used for better maintaining log file folder space .

 The expression we need to Use is

@[User::SSIS_ErrorLog_Folder] + @[System::PackageName]+ "_" +
(DT_WSTR,4)YEAR(DATEADD( "month",- 2, getdate() )) + "-" + RIGHT("0" + (DT_WSTR,2)MONTH(DATEADD( "month",- 2, getdate() )), 2)+ ".log"

\\SurendraThota\Dev\Logs\Package_2014-01.log


now we have created all the required variables

next we need to create Script task to Create Logfile if not exists and Delete if the log file exists after redention period . Use the below script to achieve this


Public Sub Main()

If (File.Exists(Dts.Variables("LogFileName").Value)) Then
         Dts.TaskResult = ScriptResults.SuccessElseFile.Create(Dts.Variables("LogFileName").Value)
End If
   If (File.Exists(Dts.Variables("DeleteLogFileName").Value)) Then
     File.Delete(Dts.Variables("DeleteLogFileName").Value)
End IfDts.TaskResult = ScriptResults.SuccessEnd Sub
End Class



 

Comments

Popular posts from this blog

SSAS : A connection cannot be made to redirector. Ensure that 'SQL Browser' service is running. (Microsoft.AnalysisServices.AdomdClient). + Solution

SSIS Error : Unable to infer the XSD from the XML file. The XML contains multiple namespaces