Own target - cannot get all log data

2 messages Options
Embed this post
Permalink
al789

Own target - cannot get all log data

Reply Threaded More More options
Print post
Permalink
Hello,

I want to store my logs in a table in oracle, having normal fields, plus an xml field that will contain as many pairs key/value as needed (this will probably evolve). For this I created my own target to store the data into oracle, and creating the xml string about a dictionary that is passed in the parameters. This is working, but shouldn't the context be used for this ? But in my target nothing is available in the context even if I pass things to it previously.  Same thing for the log data, I cannot retrieve the log data that can be configured using layout renderer (for example thread id). There is only a limited number of data in the logEvent (message, ...), how can I get the other from my target ?  Below are some lines of code for example (and probably there are more efficient way to do that).

[Target("DBX")]    
public sealed class DBXTarget : TargetWithLayout {
       
        private string connectionString;
        private string query;
        private string application;
...
        protected override void Write(LogEventInfo logEvent) {
                foreach (object o1 in o) {
                    if (o1 is Hashtable) {
                        xmlParam = this.CreateXml(Hashtable)o1);
                      ....
                 this.query = "insert into APPLICATIONLOGS values(SAPPLOG.NEXTVAL, " +
                         "to_date('" + logEvent.TimeStamp + "','dd.mm.yyyy hh24:mi:ss')" + ", '" +
                         this.Application + "', '" +
                         logEvent.Level.Name + "', '" +
                         logEvent.Message + "', '" +
                         logEvent.LoggerName + "', '" +
                         exception + "', '" +
                         stackTrace + "', '";
                         this.query += xmlParam + "')";
        }
}


Many thanks for your help.
Al
Jaroslaw Kowalski

Re: Own target - cannot get all log data

Reply Threaded More More options
Print post
Permalink
You normally get all the contextual data you need from layout renderers. Database or Mail targets can be used as examples of this technique. You simply declare a property which is of type "Layout" (say MyProp) and assign a string such as "${callsite}" to it in the constructor.

Now, in order to read the current value of the ${callsite} you need to call this.MyProp.GetFormattedMessage(logEvent).

Let me know if it helps,

Jarek