//serialize AWSAnalyticsEvent object to record the AWS Event to the local persistent system public static bool RecordAnalyticsEvent(InFocusAnalyticsEvent EventBean) { if (!isRecordEventAllowed()) { if (LogUtils != null) { LogUtils.Info(typeof(InFocusAnalyticsEventLib) + ": " + "Record Event is not allowed, record events failed!"); } return(false); } //check whether ClientName and EventName of AWSAnalyticsEvent has been set or not. if (EventBean.EventName == null) { if (LogUtils != null) { LogUtils.Error(typeof(InFocusAnalyticsEventLib) + ": " + "Record Event exception: EventName has not been set for this AWS Event!"); } return(false); } EventBean.EventTime = DateTime.UtcNow.ToString("yyyy-MM-dd-HH:mm:ss"); String RecordFile = Guid.NewGuid().ToString(); try { //step1: serialize AWSAnalyticsEvent object to string MemoryStream stream = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(InFocusAnalyticsEvent)); ser.WriteObject(stream, EventBean); stream.Position = 0; StreamReader sr = new StreamReader(stream); String AWSAnalyticsEventData = sr.ReadToEnd(); stream.Close(); stream.Dispose(); //Console.Write("JSON form of Person object: "); //Console.WriteLine(AWSAnalyticsEventData); if (LogUtils != null) { LogUtils.Info(typeof(InFocusAnalyticsEventLib) + ": " + "Begin to write Event message file " + RecordFile + " to staging folder."); LogUtils.Info(typeof(InFocusAnalyticsEventLib) + ": " + "File Content is: " + AWSAnalyticsEventData); } //step2: write event data to event message file in the staging folder. using (System.IO.StreamWriter file = new System.IO.StreamWriter(StagingPath + RecordFile, true)) { file.WriteLine(AWSAnalyticsEventData); } if (LogUtils != null) { LogUtils.Info(typeof(InFocusAnalyticsEventLib) + ": " + "Begin to move Event message file " + RecordFile + " from staging folder to production folder."); } //step3: move event message file from the staging folder to product folder. System.IO.File.Move(StagingPath + RecordFile, ProductionPath + RecordFile); if (LogUtils != null) { LogUtils.Info(typeof(InFocusAnalyticsEventLib) + ": " + "Successfully moving Event message file " + RecordFile + " from staging folder to production folder.\r\n"); } } catch (Exception ex) { if (LogUtils != null) { LogUtils.Error(typeof(InFocusAnalyticsEventLib) + ": " + "Failed to record Analytics Event into file " + RecordFile + "! Exception: " + ex); } return(false); } if (th != null && !th.IsAlive) { if (!SetProductionFolderTrimThread()) { if (LogUtils != null) { LogUtils.Error(typeof(InFocusAnalyticsEventLib) + ": " + "SetProductionFolderTrimThread Failed."); } } } return(true); }
//unit test public static void TestRecordEvent(String EventName) { string xml = @"<log4net> <root> <appender-ref ref='FileAppender' /> <!--<appender-ref ref='UdpAppender'/>--> </root> <appender name='UdpAppender' type='log4net.Appender.UdpAppender'> <threshold value='ALL' /> <remoteAddress value='255.255.255.255' /> <remotePort value='8008' /> <layout type='log4net.Layout.XmlLayoutSchemaLog4j, log4net'> <locationInfo value='true' /> </layout> </appender> <appender name='FileAppender' type='log4net.Appender.RollingFileAppender'> <threshold value='INFO' /> <file value='logs\log.txt' /> <appendToFile value='true' /> <maximumFileSize value='10240KB' /> <maxSizeRollBackups value='29' /> <param name='StaticLogFileName' value='true'/> <param name='RollingStyle' value='Size' /> <layout type='log4net.Layout.PatternLayout'> <conversionPattern value='%date [%thread] %-5level %logger %ndc - %message%newline' /> </layout> </appender> </log4net>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); log4net.Config.XmlConfigurator.Configure(doc.DocumentElement); ILog LogUtils_2 = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); //SetupLogger(LogUtils_2); if (LogUtils != null) { LogUtils.Info(typeof(InFocusAnalyticsEventLib) + ": " + "Begin to Record Event: " + EventName); } InFocusAnalyticsEvent AWSCustomEvent = new InFocusAnalyticsEvent(); AWSCustomEvent.EventName = EventName; AWSCustomEvent.AttributeMap = new Dictionary <String, String>(); AWSCustomEvent.AttributeMap.Add("RoomName", "8942"); AWSCustomEvent.AttributeMap.Add("Test", "2026"); AWSCustomEvent.AttributeMap.Add("StartTime", "20:02"); AWSCustomEvent.AttributeMap.Add("EndTime", "20:22"); AWSCustomEvent.AttributeMap.Add("Duration", "20"); AWSCustomEvent.AttributeMap.Add("NewlyAdded", "10082"); AWSCustomEvent.MetricMap = new Dictionary <String, Double>(); AWSCustomEvent.MetricMap.Add("HistoryDataSize", 2002); if (!RecordAnalyticsEvent(AWSCustomEvent)) { if (LogUtils != null) { LogUtils.Error(typeof(InFocusAnalyticsEventLib) + ": " + "Failed to Record Event!"); } } }