-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dotnet] Add examples for logging (#1541)
Add examples for dotnet logging
- Loading branch information
1 parent
b0d6545
commit 27e3638
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
examples/dotnet/SeleniumDocs/Troubleshooting/LoggingTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using OpenQA.Selenium; | ||
using OpenQA.Selenium.Internal.Logging; | ||
using OpenQA.Selenium.Remote; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace SeleniumDocs.Troubleshooting | ||
{ | ||
[TestClass] | ||
public class LoggingTest | ||
{ | ||
[TestMethod] | ||
public void Logging() | ||
{ | ||
Log.SetLevel(LogEventLevel.Trace); | ||
|
||
// TODO: Replace it with coming FileLogHandler | ||
var testLogHandler = new TestLogHandler(); | ||
Log.Handlers.Add(testLogHandler); | ||
|
||
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"); | ||
|
||
Assert.IsTrue(testLogHandler.Messages.Contains("this is a warning")); | ||
Assert.IsTrue(testLogHandler.Messages.Contains("this is useful information")); | ||
Assert.IsTrue(testLogHandler.Messages.Contains("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 }); | ||
} | ||
|
||
class TestLogHandler : ILogHandler | ||
{ | ||
public ILogHandler Clone() | ||
{ | ||
return this; | ||
} | ||
|
||
public void Handle(LogEvent logEvent) | ||
{ | ||
Messages.Add(logEvent.Message); | ||
} | ||
|
||
public List<string> Messages { get; } = new List<string>(); | ||
} | ||
} | ||
} |