public TraceEntry(string source, string message, object data, TraceEntryType entryType)
		{
			_toString = null;
			_stackTrace = string.Empty;
			_source = source == null ? string.Empty : source.Trim();
			_message = message ?? string.Empty;
			_data = data;
			_entryType = entryType;
			_timestamp = DateTime.Now;
		}
        public void Log(TraceEntryType type, string operationType, string message, string detailedMessage = null)
        {
            var item = new TraceEntryBase
            {
                Type            = type,
                OperationType   = operationType,
                Message         = message,
                DetailedMessage = detailedMessage
            };

            Log(item);
        }
		private static System.Diagnostics.EventLogEntryType GetEventLogEntryType(TraceEntryType entryType)
		{
			switch(entryType)
			{
				case TraceEntryType.Error:
					return System.Diagnostics.EventLogEntryType.Error;
				case TraceEntryType.Failure:
				case TraceEntryType.Warning:
					return System.Diagnostics.EventLogEntryType.Warning;
				case TraceEntryType.Tracing:
					return System.Diagnostics.EventLogEntryType.Information;
			}

			return System.Diagnostics.EventLogEntryType.Information;
		}
示例#4
0
		public bool Assert(bool condition, string source, string message, object data, TraceEntryType entryType)
		{
			if(!condition)
				this.Trace(source, message, data, entryType);

			return !condition;
		}
示例#5
0
		public bool Assert(bool condition, string source, string message, TraceEntryType entryType)
		{
			return this.Assert(condition, source, message, null, entryType);
		}
示例#6
0
		protected virtual TraceEntry CreateEntry(string source, string message, object data, TraceEntryType entryType)
		{
			if(string.IsNullOrWhiteSpace(source))
				source = System.Reflection.Assembly.GetCallingAssembly().GetName().Name;

			return new TraceEntry(source, message, data, entryType);
		}
示例#7
0
		public void Trace(string source, string message, object data, TraceEntryType entryType)
		{
			TraceEntry entry = this.CreateEntry(source, message, data, entryType);

			if(entry != null)
				this.Trace(entry);
		}
示例#8
0
		public void Trace(string source, string message, TraceEntryType entryType)
		{
			this.Trace(source, message, null, entryType);
		}
示例#9
0
        private static void WriteMessage(StackFrame stack, long timeTicks, TraceEntryType entryType, string message, Exception exception = null, long elapsedTicks = -1)
        {
            try
            {
                int threadId = Thread.CurrentThread.ManagedThreadId;

                Action traceJob = () =>
                {
                    var traceState = new TraceState()
                    {
                        EntryType    = entryType,
                        TimeTicks    = timeTicks,
                        ThreadId     = threadId,
                        Method       = stack.GetMethod(),
                        Message      = message,
                        ElapsedTicks = elapsedTicks,
                    };

                    LogLevel logLevel;
                    switch (entryType)
                    {
                    case TraceEntryType.Warning:
                    {
                        logLevel = LogLevel.Warning;
                        break;
                    }

                    case TraceEntryType.Error:
                    {
                        logLevel = LogLevel.Critical;
                        break;
                    }

                    case TraceEntryType.Debug:
                    case TraceEntryType.Timing:
                    case TraceEntryType.EnteringMethod:
                    case TraceEntryType.LeavingMethod:
                    {
                        logLevel = LogLevel.Debug;
                        break;
                    }

                    default:
                    {
                        logLevel = LogLevel.Information;
                        break;
                    }
                    }

                    try
                    {
                        Logger.Log(logLevel, new EventId(), traceState, exception, (state, err) =>
                        {
                            string messageToTrace;

                            if (err == null)
                            {
                                messageToTrace = state.ToString();
                            }
                            else
                            {
                                messageToTrace = string.Join("\r\n", state.ToString(), err.GetDetailsFromException());
                            }

                            return(messageToTrace);
                        });
                    }
                    catch (Exception err)
                    {
                        string anExceptionDetails = err.GetDetailsFromException();
                        Console.WriteLine("Trace failed to log to logger. " + anExceptionDetails);
                    }
                };

                EnqueueJob(traceJob);
            }
            catch (Exception err)
            {
                // Note: In case the tracing fails, the error should not be propagated to the application.
                //       Therefore, the exception is ignored.
                string anExceptionDetails = err.GetDetailsFromException();
                Console.WriteLine("Trace failed to trace the message. " + anExceptionDetails);
            }
        }