예제 #1
0
        private static Stack GetLogicalOperationStack()
        {
            if (!Tracer.IsTracingAvailable())
            {
                return(null);
            }

            try
            {
                return((Stack)Trace.CorrelationManager.LogicalOperationStack.Clone());
            }
            catch (SecurityException)
            {
                return(null);
            }
        }
예제 #2
0
 private void InitializeActivityId()
 {
     if (Tracer.IsTracingAvailable())
     {
         try
         {
             this.ActivityId = GetActivityId();
         }
         catch (Exception)
         {
             this.ActivityId = Guid.Empty;
         }
     }
     else
     {
         this.ActivityId = Guid.Empty;
     }
 }
예제 #3
0
        static void AddTracingCategories(LogEntry log,
                                         bool replacementDone)
        {
            Stack logicalOperationStack;

            if (!Tracer.IsTracingAvailable())
            {
                return;
            }

            try
            {
                logicalOperationStack = GetLogicalOperationStack();
            }
            catch (SecurityException)
            {
                return;
            }

            // add tracing categories
            foreach (object logicalOperation in logicalOperationStack)
            {
                // ignore non string objects in the stack
                string category = logicalOperation as string;
                if (category != null)
                {
                    // must take care of logging categories..
                    if (!log.Categories.Contains(category))
                    {
                        if (!replacementDone)
                        {
                            log.Categories  = new List <string>(log.Categories);
                            replacementDone = true;
                        }
                        log.Categories.Add(category);
                    }
                }
            }

            return;
        }
예제 #4
0
        /// <summary>
        /// Set the intrinsic properties such as MachineName and UserIdentity.
        /// </summary>
        private void CollectIntrinsicProperties()
        {
            this.TimeStamp = DateTime.UtcNow;

            if (Tracer.IsTracingAvailable())
            {
                try
                {
                    this.ActivityId = GetActivityId();
                }
                catch (Exception)
                {
                    this.ActivityId = Guid.Empty;
                }
            }
            else
            {
                this.ActivityId = Guid.Empty;
            }

            // do not try to avoid the security exception, as it would only duplicate the stack walk
            try
            {
                MachineName = Environment.MachineName;
            }
            catch (Exception e)
            {
                this.MachineName = String.Format(Properties.Resources.Culture, Properties.Resources.IntrinsicPropertyError, e.Message);
            }

            try
            {
                appDomainName = AppDomain.CurrentDomain.FriendlyName;
            }
            catch (Exception e)
            {
                appDomainName = String.Format(Properties.Resources.Culture, Properties.Resources.IntrinsicPropertyError, e.Message);
            }

            // check whether the unmanaged code permission is available to avoid three potential stack walks
            bool unmanagedCodePermissionAvailable      = false;
            SecurityPermission unmanagedCodePermission = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);

            // avoid a stack walk by checking for the permission on the current assembly. this is safe because there are no
            // stack walk modifiers before the call.
            if (SecurityManager.IsGranted(unmanagedCodePermission))
            {
                try
                {
                    unmanagedCodePermission.Demand();
                    unmanagedCodePermissionAvailable = true;
                }
                catch (SecurityException)
                { }
            }

            if (unmanagedCodePermissionAvailable)
            {
                try
                {
                    processId = GetCurrentProcessId();
                }
                catch (Exception e)
                {
                    processId = String.Format(Properties.Resources.Culture, Properties.Resources.IntrinsicPropertyError, e.Message);
                }

                try
                {
                    processName = GetProcessName();
                }
                catch (Exception e)
                {
                    processName = String.Format(Properties.Resources.Culture, Properties.Resources.IntrinsicPropertyError, e.Message);
                }

                try
                {
                    win32ThreadId = GetCurrentThreadId();
                }
                catch (Exception e)
                {
                    win32ThreadId = String.Format(Properties.Resources.Culture, Properties.Resources.IntrinsicPropertyError, e.Message);
                }
            }
            else
            {
                processId = String.Format(Properties.Resources.Culture,
                                          Properties.Resources.IntrinsicPropertyError,
                                          Properties.Resources.LogEntryIntrinsicPropertyNoUnmanagedCodePermissionError);
                processName = String.Format(Properties.Resources.Culture,
                                            Properties.Resources.IntrinsicPropertyError,
                                            Properties.Resources.LogEntryIntrinsicPropertyNoUnmanagedCodePermissionError);
                win32ThreadId = String.Format(Properties.Resources.Culture,
                                              Properties.Resources.IntrinsicPropertyError,
                                              Properties.Resources.LogEntryIntrinsicPropertyNoUnmanagedCodePermissionError);
            }

            try
            {
                threadName = Thread.CurrentThread.Name;
            }
            catch (Exception e)
            {
                threadName = String.Format(Properties.Resources.Culture, Properties.Resources.IntrinsicPropertyError, e.Message);
            }
        }