-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathLoggingTest.cs
80 lines (64 loc) · 2.47 KB
/
LoggingTest.cs
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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Internal.Logging;
using OpenQA.Selenium.Remote;
using System;
using System.IO;
namespace SeleniumDocs.Troubleshooting
{
[TestClass]
public class LoggingTest
{
private const string filePath = "Selenium.log";
[TestMethod]
public void Logging()
{
Log.SetLevel(LogEventLevel.Trace);
Log.Handlers.Add(new FileLogHandler(filePath));
Log.SetLevel(typeof(RemoteWebDriver), LogEventLevel.Debug);
Log.SetLevel(typeof(SeleniumManager), LogEventLevel.Info);
Warn("this is a warning");
Info("this is useful information");
Debug("this is detailed debug information");
using (var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var streamReader = new StreamReader(fileStream))
{
var fileLogContent = streamReader.ReadToEnd();
StringAssert.Contains(fileLogContent, "this is a warning");
StringAssert.Contains(fileLogContent, "this is useful information");
StringAssert.Contains(fileLogContent, "this is detailed debug information");
}
}
}
[TestCleanup]
public void TestCleanup()
{
// reset log to default
Log.SetLevel(LogEventLevel.Info)
.Handlers.Clear()
.Handlers.Add(new ConsoleLogHandler());
}
// logging is only for internal usage
// hacking it via reflection
private void Debug(string message)
{
LogMessage("Debug", message);
}
private void Warn(string message)
{
LogMessage("Warn", message);
}
private void Info(string message)
{
LogMessage("Info", message);
}
private void LogMessage(string methodName, string message)
{
var getLoggerMethod = typeof(Log).GetMethod("GetLogger", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic, new Type[] { typeof(Type) });
var logger = getLoggerMethod.Invoke(null, new object[] { typeof(LoggingTest) });
var emitMethod = logger.GetType().GetMethod(methodName);
emitMethod.Invoke(logger, new object[] { message });
}
}
}