Sorry I forgot to mention : My application is multithreaded and the log4net version = 1.2.12
On Thu, Jul 24, 2014 at 10:32 AM, Ananth Tatachar <ananth.tatac...@gmail.com > wrote: > Hi Dominik Psenner , > > Thanks a lot for replying , I am really sorry about my bad explanation of > my problem. > > I am trying to create a custom Json Layout , > > > ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > 1) The custom layout class: > > > class MyJsonLayout:LayoutSkeleton > { > private HashSet<String> IncludeFieldsList { get; set; } > > public override void ActivateOptions() > { > > } > > public override void Format(TextWriter writer, LoggingEvent > loggingEvent) > { > if(writer == null || loggingEvent == null) > { > throw new ArgumentNullException(); > } > var myJsonObj = new MyJsonObject(loggingEvent, > IncludeFieldsList ); > var json = > JsonConvert.SerializeObject(myJsonObj,Formatting.None); > writer.WriteLine(json); > } > } > > > ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > > 2) The MyJsonObject class: > > [JsonObject(MemberSerialization.OptIn)] > class MyJsonObject > { > [JsonProperty("@timestamp")] > private DateTime TimeStamp { get; set; } > > [JsonProperty("@message", NullValueHandling = > NullValueHandling.Ignore)] > private String Message { get; set; } > > [JsonProperty("@tid", NullValueHandling = > NullValueHandling.Ignore)] > private String Tid {get;set;} > > [JsonObject(MemberSerialization.OptIn)] > private String MyGuid {get;set;} > > public MyJsonObject(LoggingEvent loggingEvent) > { > var lgData = loggingEvent.GetLoggingEventData(); // For some > reason if I dont do this , all the loggingEvent.Properties are null. I did > not understand this. > TimeStamp = loggingEvent.TimeStamp; > MyGuid = loggingEvent.Properties["myguid"]; > //loggingEvent.Properties.Remove("myguid"); I tried doing this > , but nothing changed in my output. > Message = loggingEvent.Properties["message"]; > Tid = loggingEvent.Properties["tid"]; > } > > } > > > -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > > 3) The Place where I put data into log4net.ThreadContext.Properties: > > class MyInstatntiationClass > { > log4net.ThreadContext.Properties["myguid"] = > Guid.NewGuid().ToString(); > log4net.ThreadContext.Properties["tid"] > = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(); > } > > > > --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > > 4) My Web.config file: > > > > <appender name="RollingLocalFileAppender" > type="log4net.Appender.RollingFileAppender"> > <file type="log4net.Util.PatternString" value="C:\Logs\JSON.log" /> > <rollingStyle value="Date" /> > <datePattern value="yyyy-MM-dd" /> > <maxSizeRollBackups value="10" /> > <appendToFile value="true" /> > <layout type="MyJsonLayout, Version=1.0.0.0, Culture=neutral"> > </layout> > </appender> > > > > -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > > 5) Output file: > > > {"@timestamp":"2014-07-24T10:20:32.1223183-04:00" > ,"@tid":"5","@myguid":dfg134xcd_dfe-123uier00"} > {"@timestamp":"2014-07-24T10:20:32.3213382-04:00","@tid":"5","@myguid": > dfg134xcd_dfe-123uier00"} > {"@timestamp":"2014-07-24T10:20:32.8273888-04:00","@tid":"5","@myguid": > dfg134xcd_dfe-123uier00"} > {"@timestamp":"2014-07-24T10:20:32.8273888-04:00","@tid":"6 > ","@myguid":fgsg4678xzv-adfry-2356i"} > > > Expected behaviour: > > Each of my calls should result in unique guids. But as you can see I am > getting stale guids. > > > > --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > > > I have two concerns : > > 1) As you can see in the output file the guid are stale. While executing I > carefully monitered the place where guid is being generated : in file > number 3 in this file: MyInstatntiationClass. > All the generated guids were unique , however while logging I got stale > data. Also One other thing I noticed was , when I put the current thread > to sleep for 50 ms , I see valid unique guids in the output. > I believe old thread data is being used. > > > 2) I did not understand the use of LoggingEventData. In file number 2 : > MyJsonObject , If I dont make this call :var lgData = > loggingEvent.GetLoggingEventData(); I get null values from > loggingEvent.Properties. > > > > > Thanks a lot for your help in advance ... > > > > > > > > > > > > > > > > On Thu, Jul 24, 2014 at 2:40 AM, Dominik Psenner <dpsen...@gmail.com> > wrote: > >> Hi Ananth Tatachar, >> >> I'm having a hard time understanding your problem. Can you provide us a >> sample application including all the code together with both a broken >> sample output and a expected sample output? It would also be interesting >> which exact version of log4net you are using. >> >> Best regards >> >> >> >> 2014-07-24 2:31 GMT+02:00 Ananth Tatachar <ananth.tatac...@gmail.com>: >> >> >>> >>> ---------- Forwarded message ---------- >>> From: Ananth Tatachar <ananth.tatac...@gmail.com> >>> Date: Wed, Jul 23, 2014 at 8:24 PM >>> Subject: Log4net.ThreadContext.Properties Having stale data. How to >>> resolve it? >>> To: log4net-user@logging.apache.org >>> >>> >>> I am using a RollingFileAppender , and my own custom layout. My >>> application is multithreaded. Each thread will generate unique random data >>> and is supposed to log it using log4net framework. However if I create >>> multiple threads in quick succession I notice stale data in my logs , this >>> goes away if each thread sleeps for 50 ms before logging. I am not able to >>> figure out a solution to this issue or find its cause please do help me. >>> >>> My logging function looks like this: >>> >>> >>> log4net.ThreadContext.properties["randomGeneratedData"] = >>> randomGeneratedData; >>> private ILog myLog = >>> LogManager.GetLogger(typeof(LogTest)); >>> using (log4net.NDC.Push("ndcTest1")) >>> { >>> using (log4net.NDC.Push("ndcTest2")) >>> { >>> myLog.Info("myLogTest"); >>> } >>> } >>> >>> >>> >>> >>> >>> Result of logging: >>> >>> NDC = "ndcTest1 ndcTest2" , randomGeneratedData = rad1 >>> NDC = "ndcTest1 ndcTest2" , randomGeneratedData = rad2 >>> NDC = "", randomGeneratedData = rad2 >>> >>> and so on ... >>> >>> >>> I tried to clear log4net.ThreadContext.Properties before filling it >>> again , but it was of no use. Please help. >>> >>> >> >> >> -- >> Dominik Psenner >> > >