我创建自定义Appender(使用AppenderSkeleton)连接到Web服务...在这个Appender中我需要向Web服务发送一些自定义属性(如Url,Browser,User,...)... 但是当我添加这样的属性时:
ThreadContext.Properties["addr"] = System.Web.HttpContext.Current.Request.UserHostAddress;
在我这样的appender中:
public class UrlLogAppender : AppenderSkeleton
{
public string APIkey { get; set; }
public string CustomerName { get; set; }
protected override void Append(LoggingEvent loggingEvent)
{
try
{
Base.LogToDataBase.WebService1 LogtoWebserver = new Base.LogToDataBase.WebService1();
string Result = LogtoWebserver.Log(CustomerName, APIkey, loggingEvent.TimeStamp.ToString(), loggingEvent.ThreadName.ToString(), loggingEvent.Level.ToString(), loggingEvent.LoggerName, loggingEvent.RenderedMessage, loggingEvent.ExceptionObject.InnerException.Message.ToString(), loggingEvent.Properties["addr"].ToString(), loggingEvent.Properties["browser"].ToString(), loggingEvent.Properties["url"].ToString());
if (Result != "UnSucced!!")
{
//Say Excellent.... !
}
else
{
//Say Opps....!!
}
}
catch (Exception ex)
{
throw new ApplicationException("An error occured while invoking the Logging REST API", ex);
}
}
public UrlLogAppender()
{
//
// TODO: Add constructor logic here
//
}
}
}`
我无法抓住我之前添加和填充的属性(**loggingEvent.Properties["browser"].ToString()**
?!?!?!!?)
我的Web.Config是这样的:
<appender name="UrlAppender" type="log4net.Extensions.UrlLogAppender">
<threshold value="ALL"/>
<CallingApp value="Base" />
<datePattern value="_yyyy-MM-dd.lo'g'"/>
<APIkey value="321" />
<CustomerName value="Kanon" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date User:%identity IP:%X{addr} Browser: %X{browser} Url: %X{url} [%thread] %-5level %c:%m%n"/>
</layout>
</appender>
请帮帮我!
答案 0 :(得分:16)
您有两种方法可以访问已在XML文件中配置的属性。如果您有这样的配置:
<appender name="Test" type="MyAppender, MyAssembly">
<intProperty>1234</intProperty>
<stringProperty>hello</stringProperty>
<layout> ... </layout>
</appender>
您的Appender类可能如下所示:
public class MyAppender : AppenderSkeleton
{
public int IntProperty { get; set; }
public override void Append(LoggingEvent loggingEvent)
{
// implement your functionality
}
public void AddStringProperty(string value)
{
// do whatever has to be done
}
}
您可以指定与配置文件中的XML元素同名的公共属性,或者提供一个只接受一个参数的公共方法(XML元素的值),并命名为Add<name of XML element>
。 log4net将在你的appender类中查找其中一个(case是不变的)并在appender初始化时调用它们。
答案 1 :(得分:3)
最后我找到了它..... :) 我必须把这些行放在MyAppender(UrlAppender)中:
public class UrlLogAppender : AppenderSkeleton
{
public string APIkey { get; set; }
public string CustomerName { get; set; }
protected override void Append(LoggingEvent loggingEvent)
{
try
{
Base.LogToDataBase.WebService1 LogtoWebserver = new Base.LogToDataBase.WebService1();
loggingEvent.Properties["addr"] = System.Web.HttpContext.Current.Request.UserHostAddress;
loggingEvent.Properties["browser"] = System.Web.HttpContext.Current.Request.Browser.Browser + " : " + System.Web.HttpContext.Current.Request.Browser.Version;
loggingEvent.Properties["url"] = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
}
}
}
并使用它:
LogtoWebserver.Log(CustomerName, APIkey, loggingEvent.TimeStamp.ToString(),
loggingEvent.ThreadName.ToString(), loggingEvent.Level.ToString(),
loggingEvent.LoggerName, loggingEvent.RenderedMessage,
loggingEvent.ExceptionObject.InnerException.Message.ToString(),
loggingEvent.Properties["addr"].ToString(),
loggingEvent.Properties["browser"].ToString(),
loggingEvent.Properties["url"].ToString());
答案 2 :(得分:-1)
我认为您可能需要创建自定义AppenderParameters,以便可以通过您在PatternLayout中使用的值来映射它们。看看这个example - 尝试仅添加一个参数,然后在成功时添加其余参数。此外,尝试打开log4net自己的跟踪 - 它可以帮助您查明问题区域。