/// <inheritdoc/>
        public bool ShouldTrace(PlatformTraceLevel traceLevel)
        {
            if (this.DoNotInitialize)
            {
                return(false);
            }

            switch (traceLevel)
            {
            case PlatformTraceLevel.Off:
                return(false);

            case PlatformTraceLevel.Error:
                return(Source.Switch.ShouldTrace(TraceEventType.Error));

            case PlatformTraceLevel.Warning:
                return(Source.Switch.ShouldTrace(TraceEventType.Warning));

            case PlatformTraceLevel.Info:
                return(Source.Switch.ShouldTrace(TraceEventType.Information));

            case PlatformTraceLevel.Verbose:
                return(Source.Switch.ShouldTrace(TraceEventType.Verbose));

            default:
                Debug.Fail("Should never get here!");
                return(false);
            }
        }
Example #2
0
        private static void WriteAtLevel(PlatformTraceLevel level, string message)
        {
            switch (level)
            {
            case PlatformTraceLevel.Off:
                return;

            case PlatformTraceLevel.Error:
                Error(message);
                break;

            case PlatformTraceLevel.Warning:
                Warning(message);
                break;

            case PlatformTraceLevel.Info:
                Info(message);
                break;

            case PlatformTraceLevel.Verbose:
                Verbose(message);
                break;

            default:
                Debug.Fail("We should never get here!");
                break;
            }
        }
        /// <inheritdoc/>
        public void WriteLine(PlatformTraceLevel level, string message)
        {
            Debug.Assert(message != null, "message != null");
            Debug.Assert(!string.IsNullOrEmpty(ProcessName), "!string.IsNullOrEmpty(ProcessName)");

            if (EnsureTraceIsInitialized())
            {
                // The format below is a CSV so that Excel could be used easily to
                // view/filter the logs.
                var log = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}, {1}, {2:yyyy}/{2:MM}/{2:dd}, {2:HH}:{2:mm}:{2:ss}.{2:fff}, {5}, {3}, {4}",
                    ProcessId,
                    Thread.CurrentThread.ManagedThreadId,
                    DateTime.Now,
                    ProcessName,
                    message,
                    Stopwatch.GetTimestamp());

                try
                {
                    Source.TraceEvent(TraceLevelEventTypeMap[this.MapPlatformTraceToTrace(level)], 0, log);
                    Source.Flush();
                }
                catch (Exception e)
                {
                    // valid suppress
                    // Log exception from tracing into event viewer.
                    LogIgnoredException(e);
                }
            }
        }
Example #4
0
        /// <inheritdoc/>
        public bool InitializeVerboseTrace(string customLogFile)
        {
            LogFile    = Path.GetTempPath() + Path.GetFileNameWithoutExtension(customLogFile).Replace(" ", "_") + ".TpTrace.log";
            TraceLevel = PlatformTraceLevel.Verbose;

            return(this.TraceInitialized());
        }
Example #5
0
        /// <inheritdoc/>
        public void WriteLine(PlatformTraceLevel level, string message)
        {
            if (this.TraceInitialized() && TraceLevel > PlatformTraceLevel.Off)
            {
                switch (level)
                {
                case PlatformTraceLevel.Off:
                    break;

                case PlatformTraceLevel.Error:
                    UnitTestEventSource.Log.Error(message);
                    break;

                case PlatformTraceLevel.Warning:
                    UnitTestEventSource.Log.Warn(message);
                    break;

                case PlatformTraceLevel.Info:
                    UnitTestEventSource.Log.Info(message);
                    break;

                case PlatformTraceLevel.Verbose:
                    UnitTestEventSource.Log.Verbose(message);
                    break;
                }
            }
        }
Example #6
0
        public bool ShouldTrace(PlatformTraceLevel traceLevel)
        {
            if (this.DoNotInitialize)
            {
                return(false);
            }

            return((int)this.traceLevel >= (int)traceLevel);
        }
Example #7
0
        /// <summary>
        /// Initializes the tracing with custom log file and trace level.
        /// Overrides if any trace is set before.
        /// </summary>
        /// <param name="customLogFile">Custom log file for trace messages.</param>
        /// <param name="traceLevel">Trace level.</param>
        /// <returns>Trace initialized flag.</returns>
        public static bool InitializeTrace(string customLogFile, PlatformTraceLevel traceLevel)
        {
            if (!traceImpl.InitializeTrace(customLogFile, traceLevel))
            {
                ErrorOnInitialization = PlatformEqtTrace.ErrorOnInitialization;
                return(false);
            }

            return(true);
        }
Example #8
0
 public static void VerboseUnlessAlterTrace(bool condition, PlatformTraceLevel level, string format, params object[] args)
 {
     if (condition)
     {
         WriteAtLevel(level, format, args);
     }
     else
     {
         Verbose(format, args);
     }
 }
        /// <inheritdoc/>
        public bool InitializeTrace(string customLogFile, PlatformTraceLevel platformTraceLevel)
        {
            isInitialized = false;

            LogFile             = customLogFile;
            TraceLevel          = this.MapPlatformTraceToTrace(platformTraceLevel);
            Source.Switch.Level = TraceSourceLevelsMap[TraceLevel];

            // Ensure trace is initialized
            return(EnsureTraceIsInitialized());
        }
Example #10
0
        public void WriteLine(PlatformTraceLevel traceLevel, string message)
        {
            if (!this.ShouldTrace(traceLevel))
            {
                return;
            }

            var level = Enum.GetName(typeof(PlatformTraceLevel), traceLevel);

            Debug.WriteLine($"[{level}] {message}");
        }
Example #11
0
 public static void VerboseUnlessAlterTrace(bool condition, PlatformTraceLevel level, string message)
 {
     if (condition)
     {
         WriteAtLevel(level, message);
     }
     else
     {
         Verbose(message);
     }
 }
Example #12
0
 public static void InfoUnlessAlterTrace(bool condition, PlatformTraceLevel bumpLevel, string message)
 {
     if (condition)
     {
         WriteAtLevel(bumpLevel, message);
     }
     else
     {
         Info(message);
     }
 }
Example #13
0
 public static void InfoUnlessAlterTrace(bool condition, PlatformTraceLevel bumpLevel, string format, params object[] args)
 {
     if (condition)
     {
         WriteAtLevel(bumpLevel, format, args);
     }
     else
     {
         Info(format, args);
     }
 }
Example #14
0
        /// <summary>
        /// Initializes Tracing based on Trace Level
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private bool TraceInitialized()
        {
            lock (initLock)
            {
                if (isInitialized)
                {
                    return(isInitialized);
                }

                try
                {
                    var eventListener = new FileEventListener(string.IsNullOrEmpty(LogFile) ? "UnitTestLog" : LogFile);

                    PlatformTraceLevel traceLevel = this.GetTraceLevel();
                    if (traceLevel > PlatformTraceLevel.Off)
                    {
                        eventListener.EnableEvents(UnitTestEventSource.Log, EventLevel.Error);
                    }

                    if (traceLevel > PlatformTraceLevel.Error)
                    {
                        eventListener.EnableEvents(UnitTestEventSource.Log, EventLevel.Warning);
                    }

                    if (traceLevel > PlatformTraceLevel.Warning)
                    {
                        eventListener.EnableEvents(UnitTestEventSource.Log, EventLevel.Informational);
                    }

                    if (traceLevel > PlatformTraceLevel.Info)
                    {
                        eventListener.EnableEvents(UnitTestEventSource.Log, EventLevel.Verbose);
                    }

                    isInitialized = true;
                }
                catch (Exception ex)
                {
                    this.UnInitializeVerboseTrace();
                    ErrorOnInitialization = ex.Message;
                    return(false);
                }

                return(isInitialized);
            }
        }
Example #15
0
        /// <inheritdoc/>
        public bool InitializeVerboseTrace(string customLogFile)
        {
            string logFileName = string.Empty;

            try
            {
                logFileName = Path.GetFileNameWithoutExtension(customLogFile.TrimStart('"').TrimEnd('"')).Replace(" ", "_");
            }
            catch
            {
                logFileName = Guid.NewGuid().ToString();
            }

            LogFile    = Path.Combine(Path.GetTempPath(), logFileName + ".TpTrace.log");
            TraceLevel = PlatformTraceLevel.Verbose;

            return(this.TraceInitialized());
        }
        public TraceLevel MapPlatformTraceToTrace(PlatformTraceLevel traceLevel)
        {
            switch (traceLevel)
            {
            case PlatformTraceLevel.Off:
                return(TraceLevel.Off);

            case PlatformTraceLevel.Error:
                return(TraceLevel.Error);

            case PlatformTraceLevel.Warning:
                return(TraceLevel.Warning);

            case PlatformTraceLevel.Info:
                return(TraceLevel.Info);

            case PlatformTraceLevel.Verbose:
                return(TraceLevel.Verbose);

            default:
                Debug.Fail("Should never get here!");
                return(TraceLevel.Verbose);
            }
        }
Example #17
0
 private void UnInitializeVerboseTrace()
 {
     isInitialized = false;
     LogFile       = null;
     TraceLevel    = PlatformTraceLevel.Off;
 }
 /// <inheritdoc/>
 public void SetTraceLevel(PlatformTraceLevel value)
 {
     Source.Switch.Level = TraceSourceLevelsMap[this.MapPlatformTraceToTrace(value)];
 }
Example #19
0
 public void WriteLine(PlatformTraceLevel level, string message)
 {
 }
Example #20
0
 public bool ShouldTrace(PlatformTraceLevel traceLevel)
 {
     return(false);
 }
Example #21
0
 public void SetTraceLevel(PlatformTraceLevel value)
 {
     throw new NotImplementedException();
 }
Example #22
0
 public bool ShouldTrace(PlatformTraceLevel traceLevel)
 {
     throw new NotImplementedException();
 }
Example #23
0
        public bool InitializeTrace(string customLogFile, PlatformTraceLevel traceLevel)
        {
            this.traceLevel = traceLevel;

            return(false);
        }
Example #24
0
 /// <inheritdoc/>
 public void SetTraceLevel(PlatformTraceLevel value)
 {
     TraceLevel = value;
 }
Example #25
0
 /// <inheritdoc/>
 public bool ShouldTrace(PlatformTraceLevel traceLevel)
 {
     return(isInitialized);
 }
Example #26
0
 private static void WriteAtLevel(PlatformTraceLevel level, string format, params object[] args)
 {
     Debug.Assert(format != null, "format != null");
     WriteAtLevel(level, string.Format(CultureInfo.InvariantCulture, format, args));
 }
Example #27
0
 public void WriteLine(PlatformTraceLevel level, string message)
 {
     throw new NotImplementedException();
 }