예제 #1
0
        public override async void OnEntry(MethodExecutionArgs args)
        {
            var traceId = GetTraceId(args.Instance);
            var methodId = Guid.NewGuid().ToString();
            var parentMethodId = PeekAndPushParentMethodId(methodId, args.Instance);

            var windowsUser = System.Security.Principal.WindowsIdentity.GetCurrent();

            var message = new TraceMessage
            {
                TraceId = traceId,
                MethodId = methodId,
                ParentMethodId = parentMethodId,
                TraceEvent = TraceEvent.OnMethodEntry,
                Timestamp = DateTime.Now,
                MethodName = _methodName,
                Arguments = args.GetMethodArguments(),
                MachineName = Environment.MachineName,
                WindowsUsername = windowsUser != null ? windowsUser.Name : "Unknown",
            };

            message.Broadcast();

            // ensure TraceId and ParentMethodId are passed across http boundaries
            foreach (var argument in args.Arguments.Where(a => a is HttpRequestMessage))
            {
                var httpRequest = argument as HttpRequestMessage;
                if (httpRequest == null) continue;

                if (!httpRequest.Headers.Contains(TraceIdHeaderKey)) httpRequest.Headers.Add(TraceIdHeaderKey, traceId);
                if (!httpRequest.Headers.Contains(ParentMethodHeaderKey)) httpRequest.Headers.Add(ParentMethodHeaderKey, methodId);

                var httpBoundaryRequestMessage = new TraceHttpBoundaryRequestMessage
                {
                    TraceId = traceId,
                    MethodId = methodId,
                    Uri = httpRequest.RequestUri.ToString(),
                    HttpMethod = httpRequest.Method.ToString(),
                    Timestamp = DateTime.Now,
                    MachineName = Environment.MachineName,
                    Headers = httpRequest.Headers.GetHeaders(),
                    Content = (httpRequest.Content != null) ? await httpRequest.Content.ReadAsStringAsync() : "",
                };

                httpBoundaryRequestMessage.Broadcast();
            }

            args.MethodExecutionTag = new TraceAttributeContext(traceId, methodId, parentMethodId);
        }
예제 #2
0
파일: TraceHub.cs 프로젝트: csuzw/Tracer
 public void Send(TraceMessage message)
 {
     Clients.All.broadcastMessage(message);
 }
예제 #3
0
        public override async void OnSuccess(MethodExecutionArgs args)
        {
            var context = (TraceAttributeContext)args.MethodExecutionTag;
            context.Stopwatch.Stop();

            PopParentMethodId();

            var httpResponse = args.ReturnValue as HttpResponseMessage;
            if (httpResponse != null)
            {
                var httpBoundaryResponseMessage = new TraceHttpBoundaryResponseMessage
                {
                    TraceId = context.TraceId,
                    MethodId = context.MethodId,
                    Timestamp = DateTime.Now,
                    Headers = httpResponse.Headers.GetHeaders(),
                    Content = (httpResponse.Content != null) ? await httpResponse.Content.ReadAsStringAsync() : "",
                    HttpStatusCode = httpResponse.StatusCode,         
                };

                httpBoundaryResponseMessage.Broadcast();
            }

            var message = new TraceMessage
            {
                TraceId = context.TraceId,
                MethodId = context.MethodId,
                ParentMethodId = context.ParentMethodId,
                TraceEvent = TraceEvent.OnMethodSuccess,
                Timestamp = DateTime.Now,
                MethodName = _methodName,
                ReturnValue = (args.ReturnValue != null) ? args.ReturnValue.ToString() : string.Empty,
                TimeTakenInMilliseconds = context.Stopwatch.ElapsedMilliseconds
            };

            message.Broadcast();
        }
예제 #4
0
        public override void OnException(MethodExecutionArgs args)
        {
            var context = (TraceAttributeContext)args.MethodExecutionTag;
            context.Stopwatch.Stop();

            PopParentMethodId();

            var message = new TraceMessage
            {
                TraceId = context.TraceId,
                MethodId = context.MethodId,
                ParentMethodId = context.ParentMethodId,
                TraceEvent = TraceEvent.OnMethodException,
                Timestamp = DateTime.Now,
                MethodName = _methodName,
                ExceptionMessage = (args.Exception != null) ? args.Exception.Message : string.Empty,
                Exception = (args.Exception != null) ? args.Exception.ToString() : string.Empty,
                TimeTakenInMilliseconds = context.Stopwatch.ElapsedMilliseconds
            };

            message.Broadcast();
        }