2012-12-27 122 views
1

我試圖讓Enterprise Library 5.0日誌記錄到Azure SQL。我認爲我的.config是好的,但我想設置SQL表和存儲特效使用EntLib 5.0(\EntLib50Src\Blocks\Logging\Src\DatabaseTraceListener\Scripts\使用企業庫日誌記錄登錄到Azure SQL?

隨附的腳本由於腳本很長,我uploaded it here(vs嵌入它有問題)

不幸的是,該腳本是由EntLib團隊在2005年編寫的,從未更新或維護過。 Subsiquently,許多命令不針對SQL Azure中,甚至SQL Server 2012的工作,我得到SQL Server上的錯誤2012年是

Could not find stored procedure 'sp_dboption'.

是否有用於設置表和存儲過程更新腳本對於Azure SQL?另外,如果您登錄Azure SQL,將不勝感激您分享您的腳本。

+0

我不會說腳本沒有維護;該腳本適用於SQL Server,並且適用於針對企業庫的最新版本(2011年5月)進行過測試的所有SQL Server版本。版本6正處於計劃階段。 –

回答

0

我最終什麼事做的是:

  1. Downloaded Microsoft SQL Server Data Tools from here。如果您正在管理任何Azure SQL數據庫,則這是必需的。使用Schema比較和合並更改以及能夠將代碼的其餘部分推入GIT非常棒。無論如何,回到主題...

  2. 添加「SQL Server數據庫項目」到我的解決方案。

  3. 「右鍵單擊數據庫項目=>屬性=>設定的目標爲‘Windows Azure的SQL數據庫’。還檢查了‘在同一節中創建的輸出類型區域的腳本’。

  4. 右擊數據庫項目=>導入SQL =>將它從2005年的EntLib5.0腳本傳遞給它。這將腳本分解爲單獨的.SQL文件,用於每個表和每個存儲過程。

  5. Visual Studio抱怨說整個腳本不能分解(主要是存儲的特效)。它能夠導入的內容在導入的腳本中註釋掉(好,防止重複)

  6. 我手動添加了存儲的特效。添加新項目=>存儲proc =>使用在2005腳本中看到的名稱=>複製粘貼相同的舊SQL代碼。這樣做是爲了所有的4名SP在「內置」的2005年SQL腳本文件

  7. 項目=>沒有錯誤

  8. 右鍵單擊數據庫項目=>模式比較=>指向您的Azure數據庫

  9. 點擊更新

點擊更新將automatcally創建SQL腳本,它會運行對Azure數據庫。下面是生成的腳本。出於某種原因,相同的腳本無法在SQL Server 2012 Management Studio中正常運行(是的,即使在SQL CMD模式下也是如此)。儘管如此,從VS2012數據項目(上面)運行它工作得很好。

/* 
Deployment script for test 

This code was generated by a tool. 
Changes to this file may cause incorrect behavior and will be lost if 
the code is regenerated. 
*/ 

GO 
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON; 

SET NUMERIC_ROUNDABORT OFF; 


GO 
:setvar DatabaseName "Logging" 
:setvar DefaultFilePrefix "Logging" 
:setvar DefaultDataPath "" 
:setvar DefaultLogPath "" 

GO 
:on error exit 
GO 
/* 
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported. 
To re-enable the script after enabling SQLCMD mode, execute the following: 
SET NOEXEC OFF; 
*/ 
:setvar __IsSqlCmdEnabled "True" 
GO 
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True' 
    BEGIN 
     PRINT N'SQLCMD mode must be enabled to successfully execute this script.'; 
     SET NOEXEC ON; 
    END 


GO 
USE [$(DatabaseName)]; 


GO 
PRINT N'Creating [dbo].[Category]...'; 


GO 
CREATE TABLE [dbo].[Category] (
    [CategoryID] INT   IDENTITY (1, 1) NOT NULL, 
    [CategoryName] NVARCHAR (64) NOT NULL, 
    CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED ([CategoryID] ASC) 
); 


GO 
PRINT N'Creating [dbo].[CategoryLog]...'; 


GO 
CREATE TABLE [dbo].[CategoryLog] (
    [CategoryLogID] INT IDENTITY (1, 1) NOT NULL, 
    [CategoryID] INT NOT NULL, 
    [LogID]   INT NOT NULL, 
    CONSTRAINT [PK_CategoryLog] PRIMARY KEY CLUSTERED ([CategoryLogID] ASC) 
); 


GO 
PRINT N'Creating [dbo].[CategoryLog].[ixCategoryLog]...'; 


GO 
CREATE NONCLUSTERED INDEX [ixCategoryLog] 
    ON [dbo].[CategoryLog]([LogID] ASC, [CategoryID] ASC); 


GO 
PRINT N'Creating [dbo].[Log]...'; 


GO 
CREATE TABLE [dbo].[Log] (
    [LogID]   INT    IDENTITY (1, 1) NOT NULL, 
    [EventID]   INT    NULL, 
    [Priority]   INT    NOT NULL, 
    [Severity]   NVARCHAR (32) NOT NULL, 
    [Title]   NVARCHAR (256) NOT NULL, 
    [Timestamp]  DATETIME  NOT NULL, 
    [MachineName]  NVARCHAR (32) NOT NULL, 
    [AppDomainName] NVARCHAR (512) NOT NULL, 
    [ProcessID]  NVARCHAR (256) NOT NULL, 
    [ProcessName]  NVARCHAR (512) NOT NULL, 
    [ThreadName]  NVARCHAR (512) NULL, 
    [Win32ThreadId] NVARCHAR (128) NULL, 
    [Message]   NVARCHAR (1500) NULL, 
    [FormattedMessage] NTEXT   NULL, 
    CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED ([LogID] ASC) 
); 


GO 
PRINT N'Creating FK_CategoryLog_Category...'; 


GO 
ALTER TABLE [dbo].[CategoryLog] WITH NOCHECK 
    ADD CONSTRAINT [FK_CategoryLog_Category] FOREIGN KEY ([CategoryID]) REFERENCES [dbo].[Category] ([CategoryID]); 


GO 
PRINT N'Creating FK_CategoryLog_Log...'; 


GO 
ALTER TABLE [dbo].[CategoryLog] WITH NOCHECK 
    ADD CONSTRAINT [FK_CategoryLog_Log] FOREIGN KEY ([LogID]) REFERENCES [dbo].[Log] ([LogID]); 


GO 
PRINT N'Creating [dbo].[ClearLogs]...'; 


GO 
CREATE PROCEDURE ClearLogs 
AS 
BEGIN 
    SET NOCOUNT ON; 

    DELETE FROM CategoryLog 
    DELETE FROM [Log] 
    DELETE FROM Category 
END 
GO 
PRINT N'Creating [dbo].[InsertCategoryLog]...'; 


GO 
CREATE PROCEDURE InsertCategoryLog 
    @CategoryID INT, 
    @LogID INT 
AS 
BEGIN 
    SET NOCOUNT ON; 

    DECLARE @CatLogID INT 
    SELECT @CatLogID FROM CategoryLog WHERE [email protected] and LogID = @LogID 
    IF @CatLogID IS NULL 
    BEGIN 
     INSERT INTO CategoryLog (CategoryID, LogID) VALUES(@CategoryID, @LogID) 
     RETURN @@IDENTITY 
    END 
    ELSE RETURN @CatLogID 
END 
GO 
PRINT N'Creating [dbo].[WriteLog]...'; 


GO 
CREATE PROCEDURE [dbo].[WriteLog] 
(
    @EventID int, 
    @Priority int, 
    @Severity nvarchar(32), 
    @Title nvarchar(256), 
    @Timestamp datetime, 
    @MachineName nvarchar(32), 
    @AppDomainName nvarchar(512), 
    @ProcessID nvarchar(256), 
    @ProcessName nvarchar(512), 
    @ThreadName nvarchar(512), 
    @Win32ThreadId nvarchar(128), 
    @Message nvarchar(1500), 
    @FormattedMessage ntext, 
    @LogID int OUTPUT 
) 
AS 

    INSERT INTO [Log] (
     EventID, 
     Priority, 
     Severity, 
     Title, 
     [Timestamp], 
     MachineName, 
     AppDomainName, 
     ProcessID, 
     ProcessName, 
     ThreadName, 
     Win32ThreadId, 
     Message, 
     FormattedMessage 
    ) 
    VALUES (
     @EventID, 
     @Priority, 
     @Severity, 
     @Title, 
     @Timestamp, 
     @MachineName, 
     @AppDomainName, 
     @ProcessID, 
     @ProcessName, 
     @ThreadName, 
     @Win32ThreadId, 
     @Message, 
     @FormattedMessage) 

SET @LogID = @@IDENTITY 
RETURN @LogID 
GO 
PRINT N'Creating [dbo].[AddCategory]...'; 


GO 
CREATE PROCEDURE [dbo].[AddCategory] 
    -- Add the parameters for the function here 
    @CategoryName nvarchar(64), 
    @LogID int 
AS 
BEGIN 
    SET NOCOUNT ON; 
    DECLARE @CatID INT 
    SELECT @CatID = CategoryID FROM Category WHERE CategoryName = @CategoryName 
    IF @CatID IS NULL 
    BEGIN 
     INSERT INTO Category (CategoryName) VALUES(@CategoryName) 
     SELECT @CatID = @@IDENTITY 
    END 

    EXEC InsertCategoryLog @CatID, @LogID 

    RETURN @CatID 
END 
GO 
1

正式正確答案可能是從Microsoft Enterprise Library 5.0 Integration Pack for Windows Azure下載EnterpriseLibraryIntegrationPack-WindowsAzure-sqlscripts.exe。解壓後,它包含由Pattern &實踐小組修改的Enterprise Library所需的所有SQL腳本,以使用SQL Azure。

我在下面留下我的原始答案,因爲它與提供的腳本幾乎完全相同。

爲數據庫日誌記錄提供的腳本適用於SQL Server。 SQL Azure does not support the sp_dboption stored procedure

基本上,您真正需要的是Category,CategoryLog和Log表以及AddCategory,WriteLog和InsertCategoryLog存儲過程。

首先創建日誌數據庫(使用門戶網站或通過運行腳本):

CREATE DATABASE [Logging] 
(MAXSIZE=1GB, 
EDITION='web'); 
GO 

如果刪除不支持的語句的腳本會是這個樣子:

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[Category]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1) 
BEGIN 
CREATE TABLE [dbo].[Category](
    [CategoryID] [int] IDENTITY(1,1) NOT NULL, 
    [CategoryName] [nvarchar](64) NOT NULL, 
CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED 
(
    [CategoryID] ASC 
) 
) 
END 
GO 
SET ANSI_NULLS OFF 
GO 
SET QUOTED_IDENTIFIER OFF 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[CategoryLog]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1) 
BEGIN 
CREATE TABLE [dbo].[CategoryLog](
    [CategoryLogID] [int] IDENTITY(1,1) NOT NULL, 
    [CategoryID] [int] NOT NULL, 
    [LogID] [int] NOT NULL, 
CONSTRAINT [PK_CategoryLog] PRIMARY KEY CLUSTERED 
(
    [CategoryLogID] ASC 
) 
) 
END 
GO 
SET ANSI_NULLS OFF 
GO 
SET QUOTED_IDENTIFIER OFF 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[Log]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1) 
BEGIN 
CREATE TABLE [dbo].[Log](
    [LogID] [int] IDENTITY(1,1) NOT NULL, 
    [EventID] [int] NULL, 
    [Priority] [int] NOT NULL, 
    [Severity] [nvarchar](32) NOT NULL, 
    [Title] [nvarchar](256) NOT NULL, 
    [Timestamp] [datetime] NOT NULL, 
    [MachineName] [nvarchar](32) NOT NULL, 
    [AppDomainName] [nvarchar](512) NOT NULL, 
    [ProcessID] [nvarchar](256) NOT NULL, 
    [ProcessName] [nvarchar](512) NOT NULL, 
    [ThreadName] [nvarchar](512) NULL, 
    [Win32ThreadId] [nvarchar](128) NULL, 
    [Message] [nvarchar](1500) NULL, 
    [FormattedMessage] [ntext] NULL, 
CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED 
(
    [LogID] ASC 
) 
) 
END 
GO 
SET ANSI_NULLS OFF 
GO 
SET QUOTED_IDENTIFIER OFF 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[InsertCategoryLog]') AND type in (N'P', N'PC')) 
BEGIN 
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE InsertCategoryLog 
    @CategoryID INT, 
    @LogID INT 
AS 
BEGIN 
    SET NOCOUNT ON; 

    DECLARE @CatLogID INT 
    SELECT @CatLogID FROM CategoryLog WHERE [email protected] and LogID = @LogID 
    IF @CatLogID IS NULL 
    BEGIN 
     INSERT INTO CategoryLog (CategoryID, LogID) VALUES(@CategoryID, @LogID) 
     RETURN @@IDENTITY 
    END 
    ELSE RETURN @CatLogID 
END 
' 
END 
GO 
SET ANSI_NULLS OFF 
GO 
SET QUOTED_IDENTIFIER OFF 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[AddCategory]') AND type in (N'P', N'PC')) 
BEGIN 
EXEC dbo.sp_executesql @statement = N' 

CREATE PROCEDURE [dbo].[AddCategory] 
    -- Add the parameters for the function here 
    @CategoryName nvarchar(64), 
    @LogID int 
AS 
BEGIN 
    SET NOCOUNT ON; 
    DECLARE @CatID INT 
    SELECT @CatID = CategoryID FROM Category WHERE CategoryName = @CategoryName 
    IF @CatID IS NULL 
    BEGIN 
     INSERT INTO Category (CategoryName) VALUES(@CategoryName) 
     SELECT @CatID = @@IDENTITY 
    END 

    EXEC InsertCategoryLog @CatID, @LogID 

    RETURN @CatID 
END 

' 
END 
GO 
SET ANSI_NULLS OFF 
GO 
SET QUOTED_IDENTIFIER OFF 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[ClearLogs]') AND type in (N'P', N'PC')) 
BEGIN 
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE ClearLogs 
AS 
BEGIN 
    SET NOCOUNT ON; 

    DELETE FROM CategoryLog 
    DELETE FROM [Log] 
    DELETE FROM Category 
END 
' 
END 
GO 
SET ANSI_NULLS OFF 
GO 
SET QUOTED_IDENTIFIER OFF 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[WriteLog]') AND type in (N'P', N'PC')) 
BEGIN 
EXEC dbo.sp_executesql @statement = N' 



/****** Object: Stored Procedure dbo.WriteLog Script Date: 10/1/2004 3:16:36 PM ******/ 

CREATE PROCEDURE [dbo].[WriteLog] 
(
    @EventID int, 
    @Priority int, 
    @Severity nvarchar(32), 
    @Title nvarchar(256), 
    @Timestamp datetime, 
    @MachineName nvarchar(32), 
    @AppDomainName nvarchar(512), 
    @ProcessID nvarchar(256), 
    @ProcessName nvarchar(512), 
    @ThreadName nvarchar(512), 
    @Win32ThreadId nvarchar(128), 
    @Message nvarchar(1500), 
    @FormattedMessage ntext, 
    @LogId int OUTPUT 
) 
AS 

    INSERT INTO [Log] (
     EventID, 
     Priority, 
     Severity, 
     Title, 
     [Timestamp], 
     MachineName, 
     AppDomainName, 
     ProcessID, 
     ProcessName, 
     ThreadName, 
     Win32ThreadId, 
     Message, 
     FormattedMessage 
    ) 
    VALUES (
     @EventID, 
     @Priority, 
     @Severity, 
     @Title, 
     @Timestamp, 
     @MachineName, 
     @AppDomainName, 
     @ProcessID, 
     @ProcessName, 
     @ThreadName, 
     @Win32ThreadId, 
     @Message, 
     @FormattedMessage) 

    SET @LogID = @@IDENTITY 
    RETURN @LogID 



' 
END 
GO 
SET ANSI_NULLS OFF 
GO 
SET QUOTED_IDENTIFIER OFF 
GO 
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'FK_CategoryLog_Category') AND parent_obj = OBJECT_ID(N'[dbo].[CategoryLog]')) 
ALTER TABLE [dbo].[CategoryLog] WITH CHECK ADD CONSTRAINT [FK_CategoryLog_Category] FOREIGN KEY( [CategoryID]) 
REFERENCES [dbo].[Category] ( [CategoryID]) 
GO 
IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'FK_CategoryLog_Log') AND parent_obj = OBJECT_ID(N'[dbo].[CategoryLog]')) 
ALTER TABLE [dbo].[CategoryLog] WITH CHECK ADD CONSTRAINT [FK_CategoryLog_Log] FOREIGN KEY( [LogID]) 
REFERENCES [dbo].[Log] ( [LogID]) 
GO 

SET QUOTED_IDENTIFIER ON 
SET ARITHABORT ON 
SET CONCAT_NULL_YIELDS_NULL ON 
SET ANSI_NULLS ON 
SET ANSI_PADDING ON 
SET ANSI_WARNINGS ON 
SET NUMERIC_ROUNDABORT OFF 
go 

DECLARE @bErrors as bit 

BEGIN TRANSACTION 
SET @bErrors = 0 

CREATE NONCLUSTERED INDEX [ixCategoryLog] ON [dbo].[CategoryLog] ([LogID] ASC, [CategoryID] ASC) 
IF(@@error <> 0) SET @bErrors = 1 

IF(@bErrors = 0) 
    COMMIT TRANSACTION 
ELSE 
    ROLLBACK TRANSACTION 

我然後按照Managing Windows Azure SQL Database using SQL Server Management Studio使用SSMS來運行該腳本。

相關問題