我用一个例子来解释我的问题:
namespace LogQ
{
public class c1
{
public c1()
{
Console.WriteLine("C1 Cons");
LogToSql.writeLog("C1 Cons");
}
public void c1function()
{
try
{
throw new Exception();
}
catch(Exception ex)
{
Console.WriteLine("there was Exception in class c1 - method c1function");
LogToSql.writeLog(ex.ToString);
throw ex;
}
}
}
public class c2
{
public c2()
{
Console.WriteLine("C2 Cons");
LogToSql.writeLog("C2 Cons");
}
public void c2function()
{
try
{
c1 cOne = new c1();
cOne.c1function();
}
catch (Exception ex)
{
Console.WriteLine("there was Exception in class c2 - method c2function");
LogToSql.writeLog(ex.ToString);
}
}
}
public class Program
{
static void Main(string[] args)
{
for (int thread = 1; thread < 3; thread++)
{
c2 cTwo = new c2();
cTwo.c2function();
}
}
}
}
如何为每个流程编写日志 - 从开始到结束,而不与其他流程混淆?
以上示例的输出为:
C2缺点
C1缺点
在类c1中存在异常 - 方法c1function
类c2中存在异常 - 方法c2function
C2缺点
C1缺点
在类c1中存在异常 - 方法c1function
类c2中存在异常 - 方法c2function
答案 0 :(得分:1)
我假设您将日志写入sql server。所以你有一些选择。
您可以在所有logentries前加上一个像
这样的字符串LogToSql.writeLog("C1 Cons");
LogToSql.writeLog("C1 there was Exception in class c1 - method c1function");
然后你可以查询日志表,如
select * from logtable where message like 'C1%'
但是最好有一个额外的列并扩展您的日志记录,以便您可以
LogToSql.writelog(Thead.CurrentThread.ManagedThreadId, "message")
然后选择
select processId, message from logtable where processId = 12
在这种情况下,每个线程都会完成日志记录,因此很容易查询每个线程的logentries。
但是你可以使用大量的日志框架,而不是可以做类似的事情。