【问题标题】:How to send logs to GrayLog from T-SQL procedures?如何从 T-SQL 过程将日志发送到 GrayLog?
【发布时间】:2021-07-07 03:57:04
【问题描述】:

我可以使用 log4net+GELF appender 从 C# 应用程序向日志收集器发送日志。

但是如何将日志从 T-SQL 程序发送到 GrayLog? 有代码:

  1. WinForms 应用程序运行良好,我在安装了 SQL Server 的同一台机器上运行它。我看到 GrayLog 中收到的所有日志:

    private void button1_Click(object sender, EventArgs e) { 字符串设施=“DoBeDo”; 字符串主机=“我的主机名”; int 端口 = 12201;

             try
             {
                 using (var logger = new GrayLogUdpClient(facility, host, port))
                 {
                     logger.Send("Hello", "Jonny Holiday", new { Username = "John", Email = "jonny@example.com" });
                 }
             }
             catch(Exception xx)
             {
                 Console.WriteLine("***Exception:{0}", xx.Message);
             }
         }
    
  2. 有 SQLCLR 代码,它可以工作,我在 SSMS 中看到消息,但在 GrayLog 中没有任何记录,也没有任何异常:

    公共部分类 StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] 公共静态无效 SqlSPHelper(SqlString 消息) { 尝试 { SqlContext.Pipe.Send(@"SqlSPHelper:: Start"); 字符串设施=“DoBeDo”; 字符串主机=“我的主机名”; int 端口 = 12201;

             try
             {
                 using (var logger = new GrayLogUdpClient(facility, host, port))
                 {
                     logger.Send("Hey", "Donny Hooligan", new { Username = "Donald", Email = "Donny@example.com" });
                 }
             }
             catch (Exception xx)
             {
                 Console.WriteLine("***Exception:{0}", xx.Message);
             }
         }
         catch(Exception xx)
         {
             SqlContext.Pipe.Send("1:"+xx.Message);
         }
         SqlContext.Pipe.Send(@"SqlSPHelper:: Completed");
     }
    

怎么了?如何将日志发送到 GrayLog?

【问题讨论】:

    标签: sql-server tsql graylog


    【解决方案1】:

    我认为您可以通过以下几种方式做到这一点:

    1. 定义一个 CLR 存储过程,它负责记录部分
    2. 在存储过程的 CATCH 块中,RAISERROR WITH LOG 写入 Windows 事件查看器和 SQL Server 错误日志。稍后您可以从 GrayLog 中的事件查看器过滤和读取这些事件。

    第一种方法更好,因为它更干净。第二种方法使 SQL Server 错误日志有很多日志条目,这会给 DBA 团队造成误报。

    【讨论】:

    • 是的,我做了(1):创建了 SQLCLR 并对其进行了测试。它没有错误,但我在 GrayLog 服务器中看不到日志记录。缺点:要添加 log4net 和 log4net appender,我需要添加 ~15 个 NET 程序集。
    • @ZedZip,您是否正确设置了 CLR 程序集权限?另外,请调试并查看为什么它没有反映
    • 我的程序集是不安全的,我也发送调试消息,我在 SSMS 中看到它们。没有 ant 异常,但 GrayLog 中没有日志。顺便说一句,我可以附上源项目
    • 我已将代码添加到启动消息中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多