【发布时间】:2015-03-24 08:11:24
【问题描述】:
是否有任何方法可以将错误日志从事件查看器导入 SQL 表并每天安排作业。我想使用 powershell 脚本或 SSIS 包。
【问题讨论】:
标签: sql sql-server powershell ssis database-administration
是否有任何方法可以将错误日志从事件查看器导入 SQL 表并每天安排作业。我想使用 powershell 脚本或 SSIS 包。
【问题讨论】:
标签: sql sql-server powershell ssis database-administration
首先你可以很容易地在网上找到答案,但是我也想试试这个,这里是测试结果。
你可以做这样的事情:
CREATE TABLE WinLogs (
EntryType VARCHAR(255),
[Source] VARCHAR(255),
[Message] VARCHAR(4000),
TimeGenerated datetime
)
Data Flow task添加到包中;在包内添加Script Component,您应该在其中添加4个输出列(箭头显示要更改的内容):
EntryType(字符串 255)
来源(字符串 255)
消息(字符串 4000)
TimeGenerated(数据库时间戳)
public override void CreateNewOutputRows()
{
// Get all events from the Application(/System/Security) log from the local server (.)
EventLog myEvenLog = new EventLog("Application", ".");
// Create variable to store the entry
EventLogEntry myEntry;
// Loop trough all entries (oldest first)
for (int i = 0; i < myEvenLog.Entries.Count; i++)
{
// Get single entry
myEntry = myEvenLog.Entries[i];
// Add new records
this.Output0Buffer.AddRow();
// Fill columns
this.Output0Buffer.EntryType = myEntry.EntryType.ToString();
this.Output0Buffer.Source = myEntry.Source;
this.Output0Buffer.TimeGenerated = myEntry.TimeGenerated;
// Take a max of 4000 chars
this.Output0Buffer.Message = myEntry.Message.Substring(0, (myEntry.Message.Length > 4000 ? 3999 : myEntry.Message.Length - 1));
}
}
SQL Server Destination组件(也可以是OLE DB Destination)来源来自这个例子Eventlog as a source
【讨论】: