Faez Mehrabani
Faez Mehrabani

Reputation: 214

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 !

Upvotes: 7

Views: 14059

Answers (3)

Faez Mehrabani
Faez Mehrabani

Reputation: 214

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());

Upvotes: 3

Thomas Lielacher
Thomas Lielacher

Reputation: 1067

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.

Upvotes: 17

John J Smith
John J Smith

Reputation: 11923

I think you may need to create custom AppenderParameters so that they can be mapped via the values you use in the PatternLayout. Have a look at this example - try adding one parameter only and then add the rest if you are successful. Also, try switching on log4net's own tracing - it can help you pinpoint problem areas.

Upvotes: -1

Related Questions