Exemple #1
0
        /// <summary>
        ///     Called after the operation has returned but before the reply message is sent.
        /// </summary>
        /// <param name="reply">The reply message. This value is null if the operation is one way.</param>
        /// <param name="correlationState">
        ///     The correlation object returned from the
        ///     <see
        ///         cref="M:System.ServiceModel.Dispatcher.IDispatchMessageInspector.AfterReceiveRequest(System.ServiceModel.Channels.Message@,System.ServiceModel.IClientChannel,System.ServiceModel.InstanceContext)" />
        ///     method.
        /// </param>
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            if (_tracer == null)
            {
                return;
            }

            TraceContext context = TraceContext.Create(Constants.TraceParameter.Name, _webServiceName)
                                   .Add(Constants.TraceParameter.Operation, _operationName);
            string eventName = _webServiceName + "." + _operationName;
            string message   = string.Format(CultureInfo.CurrentCulture, InternalMessages.WebServiceResponse, eventName);

            _tracer.TraceStopPerformance(_serverSideStartTime, message, TraceEventType.HttpResponse, eventName, context);
        }
Exemple #2
0
        /// <summary>
        ///     Traces the specified message and execution time.
        /// </summary>
        /// <param name="message">The optional message, or the caller member if not specified.</param>
        /// <param name="traceEventType">The optional event type.</param>
        /// <param name="traceEventName">The calling context.</param>
        /// <param name="context">The optional contextual trace parameters.</param>
        /// <remarks>
        ///     If performance measure cannot be done in one method, consider using the
        ///     TraceStartPerformance/TraceStopPerformance methods instead.
        /// </remarks>
        /// <code>using (logger.TracePerformance("service call")) {...}</code>
        public IDisposable TracePerformance(string message = null, TraceEventType traceEventType = TraceEventType.CodeBlock,
                                            [System.Runtime.CompilerServices.CallerMemberName] string traceEventName = null, TraceContext context = null)
        {
            DateTime startTime = HighResolutionDateTime.UtcNow;

            if (string.IsNullOrEmpty(message))
            {
                message = string.Format(CultureInfo.CurrentCulture, InternalMessages.MethodCall, traceEventName ?? InternalMessages.EventUnknown);
            }
            return
                (new PerformanceWatch(
                     elapsedTime =>
                     TraceInternal(TraceCategory.Performance, 0, traceEventType, traceEventName, startTime, elapsedTime, null, message, context)));
        }
Exemple #3
0
        /// <summary>
        ///     Traces the specified exception.
        /// </summary>
        /// <param name="exception">The specified exception.</param>
        /// <param name="traceEventType">The optional event type.</param>
        /// <param name="errorCode">The specified code. Optional. By default it is assumed that this is a global level caught exception.</param>
        /// <param name="context">Trace contextual information.</param>
        /// <remarks>This is the preferred method to log exceptions caught at global level.</remarks>
        public void TraceException(Exception exception, TraceEventType traceEventType = TraceEventType.GlobalLevelCaughtError, int errorCode = ErrorCodes.GlobalLevelError, TraceContext context = null)
        {
            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }
            var    baseException = exception as BaseException;
            int    realCode      = baseException?.ErrorCode ?? errorCode;
            string message       = exception.Message;
            // try to find our own exceptions supposed to be more meaningfull
            Exception innerException = GetInnerException(exception, 10);

            if (innerException != exception)
            {
                message += " : " + innerException.Message;
                var internalBaseException = innerException as BaseException;
                realCode = internalBaseException?.ErrorCode ?? realCode;
            }
            var loadException = innerException as ReflectionTypeLoadException;

            if (loadException != null)
            {
                foreach (var exceptionMessage in loadException.LoaderExceptions.Select(e => e.Message).Distinct())
                {
                    message += "\r\n- " + exceptionMessage;
                }
            }

            TraceContext traceContext = context ?? TraceContext.Create(Constants.TraceParameter.ErrorCode, realCode);

            TraceInternal(TraceCategory.Error, realCode, traceEventType, null, null, null, exception, message, traceContext);
        }
Exemple #4
0
        private void TraceInternal(TraceCategory traceCategory, int errorCode, TraceEventType traceEventType, string traceEventName, DateTime?traceDate,
                                   TimeSpan?elapsedTime, Exception exception, string message, TraceContext context)
        {
            if (_traceSource == null)
            {
                throw new InvalidOperationException("SetSource() must be called before.");
            }
            // if no date were specified use now
            DateTime creationDate = traceDate ?? HighResolutionDateTime.UtcNow;

            // mapping TraceCategory System.Diagnostics.TraceEventType
            System.Diagnostics.TraceEventType dotnetTraceEventType = CategoryMapper[traceCategory];
            TraceEventData traceEvent =
                new TraceEventData
            {
                CreationDate     = creationDate,
                ContextParameter = context?.ToString(),
                CorrelationId    = GetCorrelationId(),
                SessionId        = GetSessionId(),
                UserName         = GetPrincipalId(),
                ElapsedTime      = elapsedTime,
                ErrorCode        = errorCode,
                RawException     = exception,
                TraceCategory    = traceCategory,
                TraceEventType   = traceEventType,
                TraceEventName   = traceEventName,
                TraceSourceName  = _traceSourceName,
                TraceSourceType  = _traceSourceType,
                Message          = message,
            };

            traceEvent.ComputeAutomaticProperties();
            _traceSource.TraceData(dotnetTraceEventType, errorCode, traceEvent);
        }