chr
2026-04-08 53e656200368a983e563550e2cc1acbc6d86b729
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Copyright 2012-2019 Keysight Technologies
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using System;
using System.Diagnostics;
using OpenTap;
 
namespace OpenTap.Plugins.PluginDevelopment
{
    [Display("Log Messages Example", Groups: new[] { "Examples", "Plugin Development", "Step Execution" }, 
        Description: "Example of logging.")]
    public class LogMessages : TestStep
    {
        // Users can always use the predefined Log that is defined in the TestStep base class.
        // Optionally, they can define their own log, which is useful to show different log messages sources.
        // In the case below, MyLog will show up in the logs.
        private OpenTap.TraceSource MyLog = OpenTap.Log.CreateSource("MyLog");
 
        public override void Run()
        {   
            // There are four levels of log messages Info, Warning, Error, Debug.
            MyLog.Info("Info from Run");
            for (int i = 0; i < 10; i++)
            {
                MyLog.Debug("Debug {0} from Run", i); // MyLog.X works like string.Format with regards to arguments.
            }
            MyLog.Warning("Warning from Run");
            MyLog.Error("Error from Run");
 
            // The Log can accept a Stopwatch Object to be used for timing analysis.
            Stopwatch sw1 = Stopwatch.StartNew();
            TapThread.Sleep(100);
            MyLog.Info(sw1, "Info from Run");
 
            Stopwatch sw2 = Stopwatch.StartNew();
            TapThread.Sleep(200);
            MyLog.Error(sw2, "Error from step");
 
            // Tracebar can be used to show results in the MyLog.
            var traceBar = new TraceBar();
            traceBar.LowerLimit = -3.0;
            for (var i = -2; i < 11; i++)
            {
                traceBar.UpperLimit = i < 5 ? 3 : 15;
                // GetBar returns a string with value, low limit, a dashed line 
                // indicating magnitude, the upper limit, and (if failing), a fail indicator.
                string temp = traceBar.GetBar(i);
                MyLog.Info("MyResult: " + temp);
                TapThread.Sleep(200);
            }
            // Sample output shown below.
            //   MyResult: 2.00 - 3-------------------------|-----  3
            //   MyResult: 3.00 - 3------------------------------ | 3
            //   MyResult: 4.00 - 3------------------------------ > 3  Fail
            //   MyResult: 5.00 - 3------------ -| -----------------15
            //   MyResult: 6.00 - 3-------------- -| ---------------15
 
            // TraceBar remembers if any results failed, so it can be used for the verdict.
            UpgradeVerdict(traceBar.CombinedVerdict);
 
            // The log also supports showing stack traces. 
            // Useful for debugging.
            try
            {
                throw new Exception("My exception");
            }
            catch (Exception e)
            {
                MyLog.Error("Caught exception: '{0}'", e.Message);
                MyLog.Debug(e); // Prints the stack trace to the MyLog.
            }
        }
    }
}