我有一个使用log4net AdoNetAppender(DB appender)将我的日志写入数据库的应用程序 . 它将记录写入数据库,但自定义字段都是NULL . 它们在我的单元测试中使用相同的代码工作正常,但在应用程序运行时调用它们时则不行 . 它是一个多线程应用程序,用于处理消息队列中的消息 .
是否有任何已知的问题(任何人都知道)关于具有多线程应用程序的DB appender的自定义属性?这是我唯一的猜测,为什么他们在应用程序启动时不工作,因为我无法在单元测试等中重现 .
默认的log4net字段很好 .
我在Singleton中设置自定义属性值:
public sealed class Logger
{
private static readonly Logger instance = new Logger();
public static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// Static constructor which sets the properties for the database logging using log4net. This enables analysts to view the log events
/// inside the database as well as in the local application log file.
/// </summary>
static Logger()
{
GlobalContext.Properties["Application"] = "MyApp";
GlobalContext.Properties["ApplicationVersion"] = Utility.GetApplicationVersion();
var windowsHost = System.Net.Dns.GetHostName();
if (!String.IsNullOrEmpty(windowsHost))
LogicalThreadContext.Properties["Host"] = windowsHost;
//ThreadContext.Properties["LogTime"] = DateTime.Now;
var windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();
if (windowsIdentity != null)
GlobalContext.Properties["CreatedBy"] = windowsIdentity.Name;
}
private Logger()
{
}
public static Logger Instance
{
get { return instance; }
}
}
编辑:
发现这将尝试添加额外的log4net调试 .
http://logging.apache.org/log4net/release/faq.html#internalDebug
没有看到log4nets内部日志中的任何错误,所以仍然不清楚发生了什么 .
1 回答
我终于弄明白了 . 这就是我们尝试全局设置自定义属性的方式 . 我会在我的任何实例绑定到队列和处理消息之前在app启动时设置属性 . 这似乎是这样做的......不知道为什么我在开始时没有想到这一点 . 刚刚使用了log4net GlobalContext,就像一个魅力 .
--S