public void ValidationBatchException_Serialization() { var error1 = new AzosException("error1"); var error2 = new ArgumentException("error2"); var got = ValidationBatchException.Concatenate(error1, error2); Aver.IsNotNull(got); var ser = new SlimSerializer(); var ms = new MemoryStream(); ser.Serialize(ms, got); ms.Position = 0; var deser = ser.Deserialize(ms) as ValidationBatchException; Aver.IsNotNull(deser); Aver.AreNotSameRef(got, deser); Aver.IsNotNull(deser.Batch); Aver.AreEqual(2, deser.Batch.Count); Aver.IsTrue(deser.Batch[0] is AzosException); Aver.IsTrue(deser.Batch[1] is ArgumentException); Aver.AreEqual("error1", deser.Batch[0].Message); Aver.AreEqual("error2", deser.Batch[1].Message); }
public void ValidationBatchException_Concat_06() { var error1 = new AzosException("error1"); var error2 = new ArgumentException("error2"); var got = ValidationBatchException.Concatenate(error1, error2); Aver.IsNotNull(got); var be = got as ValidationBatchException; Aver.IsNotNull(be); Aver.IsNotNull(be.Batch); Aver.AreEqual(2, be.Batch.Count); Aver.AreSameRef(error1, be.Batch[0]); Aver.AreSameRef(error2, be.Batch[1]); var got2 = ValidationBatchException.Concatenate(got, error2); var be2 = got2 as ValidationBatchException; Aver.IsNotNull(be2); Aver.IsNotNull(be2.Batch); Aver.AreEqual(3, be2.Batch.Count); Aver.AreSameRef(error1, be2.Batch[0]); Aver.AreSameRef(error2, be2.Batch[1]); Aver.AreSameRef(error2, be2.Batch[2]); }
public void ValueIsOfType() { object x = this; Aver.IsTrue(this == x.ValueIsOfType <GuardTests, object>()); Aver.IsTrue(this == x.ValueIsOfType <object, object>()); Aver.IsTrue(this == x.ValueIsOfType(typeof(GuardTests))); Aver.IsTrue(this == x.ValueIsOfType(typeof(object))); x = new AzosException(); Aver.IsTrue(x == x.ValueIsOfType <Exception, object>()); Aver.IsTrue(x == x.ValueIsOfType <object, object>()); Aver.Throws <CallGuardException>(() => x.ValueIsOfType <GuardTests, object>()); Aver.IsTrue(x == x.ValueIsOfType(typeof(object))); Aver.IsTrue(x == x.ValueIsOfType(typeof(object))); Aver.Throws <CallGuardException>(() => x.ValueIsOfType(typeof(GuardTests))); x = null; Aver.Throws <CallGuardException>(() => x.ValueIsOfType(typeof(GuardTests))); x = typeof(Exception); Aver.Throws <CallGuardException>(() => x.ValueIsOfType(typeof(GuardTests))); }
protected virtual void CleanupApplication() { var exceptions = new List <Exception>(); lock (m_FinishNotifiables) { string name = CoreConsts.UNKNOWN; foreach (var notifiable in m_FinishNotifiables) { try { name = notifiable.Name ?? notifiable.GetType().FullName; notifiable.ApplicationFinishBeforeCleanup(this); } catch (Exception error) { error = new AzosException(StringConsts.APP_FINISH_NOTIFIABLE_BEFORE_ERROR.Args(name, error.ToMessageWithType()), error); exceptions.Add(error); WriteLog(MessageType.Error, "CleanupApplication()", error.Message); } } } try { DoModuleBeforeCleanupApplication(); DoCleanupApplication(); //<---------------------------------------------- } finally { ExecutionContext.__UnbindApplication(this); } lock (m_FinishNotifiables) { string name = CoreConsts.UNKNOWN; foreach (var notifiable in m_FinishNotifiables) { try { name = notifiable.Name ?? notifiable.GetType().FullName; notifiable.ApplicationFinishAfterCleanup(this); } catch (Exception error) { error = new AzosException(StringConsts.APP_FINISH_NOTIFIABLE_AFTER_ERROR.Args(name, error.ToMessageWithType()), error); exceptions.Add(error); //log not available at this point } } } if (exceptions.Count > 0) { var text = new StringBuilder(); text.AppendLine(StringConsts.APP_FINISH_NOTIFIABLES_ERROR); foreach (var exception in exceptions) { text.AppendLine(exception.ToMessageWithType()); } throw new AzosException(text.ToString()); } }
internal void SendRegularAndFailures(Message msg) { if (!Running) { return; } //When there was failure and it was not long enough if (m_LastErrorTimestamp.HasValue) { if ((LocalizedTime - m_LastErrorTimestamp.Value).TotalMilliseconds < m_RestartProcessingAfterMs) { //this is after than throwing exception var error = new AzosException(string.Format(StringConsts.LOGSVC_SINK_IS_OFFLINE_ERROR, Name)); SetError(error, msg); return; } } try { if (!Channel.IsZero && msg.Channel != Channel) { return; } if (!applyFilterExpressions(msg)) { return; } if (m_Levels.Count > 0) { bool found = false; foreach (var r in m_Levels) { if (r.Item1 <= msg.Type && msg.Type <= r.Item2) { found = true; break; } } if (!found) { return; } } var msgLocalTime = UniversalTimeToLocalizedTime(msg.UTCTimeStamp); if ( (!m_MinLevel.HasValue || msg.Type >= m_MinLevel.Value) && (!m_MaxLevel.HasValue || msg.Type <= m_MaxLevel.Value) && (!m_DaysOfWeek.HasValue || m_DaysOfWeek.Value.Contains(msgLocalTime.DayOfWeek)) && (!m_StartDate.HasValue || msgLocalTime >= m_StartDate.Value) && (!m_EndDate.HasValue || msgLocalTime <= m_EndDate.Value) && (!m_StartTime.HasValue || msgLocalTime.TimeOfDay >= m_StartTime.Value) && (!m_EndTime.HasValue || msgLocalTime.TimeOfDay <= m_EndTime.Value) ) { if (!m_MaxProcessingTimeMs.HasValue) { DoSend(msg); } else { m_StopWatch.Restart(); DoSend(msg); m_StopWatch.Stop(); if (m_LastError != null) { m_AverageProcessingTimeMs = 0f; //reset average time to 0 after 1st successful execution after prior failure } //EMA filter m_AverageProcessingTimeMs = (PROCESSING_TIME_EMA_FILTER * m_StopWatch.ElapsedMilliseconds) + ((1.0f - PROCESSING_TIME_EMA_FILTER) * m_AverageProcessingTimeMs); if (m_AverageProcessingTimeMs > m_MaxProcessingTimeMs.Value) { throw new LogException(string.Format(StringConsts.LOGSVC_SINK_EXCEEDS_MAX_PROCESSING_TIME_ERROR, Name, m_MaxProcessingTimeMs, m_StopWatch.ElapsedMilliseconds)); } } if (m_LastError != null) { SetError(null, msg); //clear-out error } } } catch (Exception error) { //WARNING!!! //under no condition MAY any exception escape from here SetError(error, msg); } }
protected virtual void InitApplication() { if (ForceInvariantCulture)//used in all server applications { Azos.Platform.Abstraction.PlatformAbstractionLayer.SetProcessInvariantCulture(); } PreloadAssemblies(); var exceptions = new List <Exception>(); var starters = GetStarters().ToList(); string name = CoreConsts.UNKNOWN; bool breakOnError = true; foreach (var starter in starters) { try { breakOnError = starter.ApplicationStartBreakOnException; name = starter.Name ?? starter.GetType().FullName; starter.ApplicationStartBeforeInit(this); } catch (Exception error) { error = new AzosException(StringConsts.APP_STARTER_BEFORE_ERROR.Args(name, error.ToMessageWithType()), error); if (breakOnError) { throw error; } exceptions.Add(error); //log not available at this point } } ExecutionContext.__BindApplication(this); DoInitApplication(); //<---------------------------------------------- DoModuleAfterInitApplication(); name = CoreConsts.UNKNOWN; breakOnError = true; foreach (var starter in starters) { try { breakOnError = starter.ApplicationStartBreakOnException; name = starter.Name ?? starter.GetType().FullName; starter.ApplicationStartAfterInit(this); } catch (Exception error) { error = new AzosException(StringConsts.APP_STARTER_AFTER_ERROR.Args(name, error.ToMessageWithType()), error); WriteLog(MessageType.CatastrophicError, "InitApplication().After", error.ToMessageWithType(), error); if (breakOnError) { throw error; } } } if (exceptions.Count > 0) { foreach (var exception in exceptions) { WriteLog(MessageType.CatastrophicError, "InitApplication().Before", exception.ToMessageWithType()); } } }