我可以使用 log4net+GELF appender 将日志从 C# 应用程序发送到日志收集器。
但是如何将日志从 T-SQL 程序发送到 GrayLog? 有代码:
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);
}
}
有 SQLCLR 代码,它可以工作,我在 SSMS 中看到消息,但 GrayLog 中没有任何记录,也没有任何异常:
公共分部类 StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] 公共静态无效 SqlSPHelper(SqlString msg) { 尝试 { 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?
答案 0 :(得分:0)
我认为您可以通过以下几种方式做到这一点:
第一种方法更好,因为它更干净。第二种方法使SQL Server错误日志有很多日志条目,这会导致DBA团队误报。