Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Add custom properties in AppenderSkeleton log4net

I Create Custom Appender(with AppenderSkeleton) that Connect to the Web Service... in this Appender i need to send some custom properties(like Url , Browser ,User , ...) to the web Service... but when i add this properties like :

ThreadContext.Properties["addr"] = System.Web.HttpContext.Current.Request.UserHostAddress;

in my appender that like this :

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
            //
        }
    }
}`

i can't catch the properties that i added and filled before(**loggingEvent.Properties["browser"].ToString()**?!?!?!!?)

my Web.Config like this :

  <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>

please Help me !

like image 347
Faez Mehrabani Avatar asked Jun 15 '13 06:06

Faez Mehrabani


2 Answers

You have two possibilities to access properties you have configured in your XML file. If you have a config like this:

<appender name="Test" type="MyAppender, MyAssembly">
    <intProperty>1234</intProperty>
    <stringProperty>hello</stringProperty>

    <layout> ... </layout>
</appender>

Your Appender class can look like this:

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
    }
}

You can either specify a public property with the same name as the XML element from the config file, or provide a public method which takes exactly one argument (the value of the XML element) and is named like Add<name of XML element>. log4net will search for one of those in your appender class (the case is invariant) and call them when the appender is initialised.

like image 83
Thomas Lielacher Avatar answered Oct 28 '22 17:10

Thomas Lielacher


Finally i found it..... :) I have to put this lines in 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;
}
}
}

and for use it :

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());
like image 37
Faez Mehrabani Avatar answered Oct 28 '22 16:10

Faez Mehrabani