assembly Microsoft SQL Studio管理存储过程中的日志

tct7dpnv  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(64)

因为我的SSMS组装过程停止工作,我需要添加一些日志。但是,因为我是新来的,我有很大的问题,如何做到这一点,以及在哪里我可以看到这个日志。
正如你在上面的StoredProcedures类中看到的,我运行了WSClient类,它应该向WebSocket服务器发送一些数据。我想在这里添加一些console.log,并检查它是否正在运行。但我不知道该怎么做。
我的程序看起来:

using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    using Microsoft.SqlServer.Server;
    using System.Net.WebSockets;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    public partial class StoredProcedures
    {
        [Microsoft.SqlServer.Server.SqlProcedure]
        public static void HelloWorld(String value)
        {
    
            Microsoft.SqlServer.Server.SqlMetaData columnInfo
    = new Microsoft.SqlServer.Server.SqlMetaData("VALUE", SqlDbType.NVarChar, Max);
            SqlDataRecord greetingRecord
                = new SqlDataRecord(new Microsoft.SqlServer.Server.SqlMetaData[] { columnInfo });

           //logs is working ok, displays what I need, and this is ok

            greetingRecord.SetString(0, value);
            SqlContext.Pipe.Send(greetingRecord);
    
            var ws = new WSClient();

            // how to inspect WSClient and where I could see it????
            WSClient.runWSClient(value).GetAwaiter().GetResult();
        }
    }
    
    public class WSClient
    {
        public static async Task runWSClient(String value)
        {
    
    
            using (ClientWebSocket client = new ClientWebSocket())
            {
                Uri servicesUri = new Uri("wss://websocket.kawczynski.eu");
                var cTs = new CancellationTokenSource();
                cTs.CancelAfter(TimeSpan.FromSeconds(120));
                try
                {
                    await client.ConnectAsync(servicesUri, cTs.Token);
    
                    if (client.State == WebSocketState.Open)
                    {
                        string message = value;
                        //SqlContext.Pipe.Send("ws client got message: " + message);
                        if (!string.IsNullOrEmpty(message))
                        {
                            ArraySegment<byte> byteToSend = new ArraySegment<byte>(Encoding.UTF8.GetBytes(message));
                            await client.SendAsync(byteToSend, WebSocketMessageType.Text, true, cTs.Token);
    
                        }
                    }
                }
                catch (WebSocketException e)
                {
                    //SqlContext.Pipe.Send("error from hello world assembly: " + e.Message);
                    Console.WriteLine(e.Message);
                }
            }
     

   }
}
mbyulnm0

mbyulnm01#

对于类似的问题,你可以有不同的方法,
例如,我会尝试在存储过程中编写一个正确的结构,这样您就可以在问题到达代码之前轻松捕获它。
例如,你可以这样构造你的存储过程:

CREATE PROCEDURE [Report].[PpX_TestStored]

AS  
--*/  
BEGIN

SET NOCOUNT ON
--
--
BEGIN TRY

---------------------------------------------------------
    --Variable Declaration
    ---------------------------------------------------------------------------- 
 --------
    DECLARE @ProcedureName              varchar(200) = "Name"
--------------------------------------------

    ---------------------------------------------------------------------------- 
    --------

    ------------------------------------------------------------------------------------
   SELECT 1 AS returnvalue  -- PUT HERE your working code



    ------------------------------------------------------------------------------------


    ------------------------------------------------------------------------------------
    --
    --
END TRY
BEGIN CATCH
    ------------------------------------------------------------------------------------------------  
    ------------------------------------------------------------------------------------------------  
    --
    --
    ------------------------------------------------------------------------------------------------  
    --Error Handling
    ------------------------------------------------------------------------------------------------  

    DECLARE @ERROR_MESSAGE varchar(max)=ERROR_MESSAGE(),@ERROR_LINE int=ERROR_LINE(),@ERROR_PROCEDURE varchar(max)=ERROR_PROCEDURE(),@ERROR_DETAILS varchar(max)=ERROR_MESSAGE()
    --
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

    SELECT
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE();

        RAISERROR (@ERROR_MESSAGE, -- Message text.
               @ErrorSeverity, -- Severity.
               @ErrorState -- State.
               );

END CATCH;
END

使用此代码,错误将被引发,直到您自己的调用者代码的“catch块”。
此外,您可以在这个“catch块”中放置一个小语句,将错误插入数据库的表中,因此错误被保存,
类似于简单的“插入dbo.Log values(errorMsg)(@ERROR_MESSAGE)”

相关问题