Example #1
0
        public void TraceExceptionSkipsBaseMethod()
        {
            var hasBeenInvoked = false;

            var traceSourceBase = Mock.Create <Sut>();

            Mock.Arrange(() => traceSourceBase.TraceEvent(TraceEventType.Error, Arg.IsAny <int>(), Arg.IsAny <string>(), Arg.IsAny <string>(), Arg.IsAny <string>(), Arg.IsAny <string>(), Arg.IsAny <string>()))
            .IgnoreArguments()
            .IgnoreInstance()
            .DoInstead(() => hasBeenInvoked = true);

            var ex  = new ArgumentException("arbitraryArgumentException");
            var sut = new Sut("arbitraryName");

            SourceLevels[] sourceLevels =
            {
                SourceLevels.Off,
                SourceLevels.Critical,
                SourceLevels.ActivityTracing,
            };

            foreach (var sourceLevel in sourceLevels)
            {
                hasBeenInvoked   = false;
                sut.Switch.Level = sourceLevel;
                sut.TraceException(ex);
                Assert.IsFalse(hasBeenInvoked);
            }
        }
Example #2
0
        public void TraceExceptionInvokesBaseMethod()
        {
            var hasBeenInvoked = false;

            var traceSourceBase = Mock.Create <Sut>();

            Mock.Arrange(() => traceSourceBase.TraceEvent(TraceEventType.Error, Arg.IsAny <int>(), Arg.IsAny <string>(), Arg.IsAny <string>(), Arg.IsAny <string>(), Arg.IsAny <string>(), Arg.IsAny <string>()))
            .IgnoreArguments()
            .IgnoreInstance()
            .DoInstead(() => hasBeenInvoked = true);

            var ex  = new ArgumentException("arbitraryArgumentException");
            var sut = new Sut("arbitraryName");

            SourceLevels[] sourceLevels =
            {
                SourceLevels.Error,
                SourceLevels.Warning,
                SourceLevels.Information,
                SourceLevels.Verbose,
                SourceLevels.All,
            };

            foreach (var sourceLevel in sourceLevels)
            {
                hasBeenInvoked   = false;
                sut.Switch.Level = sourceLevel;
                sut.TraceException(ex);
                Assert.IsTrue(hasBeenInvoked, sourceLevel.ToString());
            }
        }
Example #3
0
        /// <summary>
        /// Sets a ContractFailed event handler for the calling assembly and trace source
        /// Calling this method multiple times for the same assembly has no effect
        /// </summary>
        /// <param name="traceSource">Sepcifies a TraceSource to send message to</param>
        public static void RegisterTraceSource(TraceSource traceSource)
        {
            Contract.Requires(null != traceSource);

            var stackFrame = new StackFrame(CALLING_FRAME);

            var declaringType = stackFrame.GetMethod().DeclaringType;
            var message       = string.Format(CALLINGFRAME_GETMETHOD_NAME_TRACESOURCE, stackFrame.GetMethod().Name, traceSource.Name);

            Contract.Assert(null != declaringType, message);

            RegisterTraceSource(declaringType.Assembly, traceSource);
        }
Example #4
0
        /// <summary>
        /// Sets a ContractFailed event handler for the specified assembly and trace source
        /// Calling this method multiple times for the same assembly has no effect
        /// </summary>
        /// <param name="assembly">Assembly for which the specified TraceSource wll be registered</param>
        /// <param name="traceSource">Sepcifies a TraceSource to send message to</param>
        public static void RegisterTraceSource(Assembly assembly, TraceSource traceSource)
        {
            Contract.Requires(null != assembly);
            Contract.Requires(null != traceSource);

            if (_traceSources.ContainsKey(assembly.FullName))
            {
                return;
            }

            _traceSources.AddOrUpdate(assembly.FullName, traceSource, (key, originalTraceSource) => originalTraceSource);

            // register event handler only once
            Contract.Assert(_isEventHandlerRegistered.Value);
        }