Skip to content
Chen edited this page Apr 13, 2018 · 4 revisions

WCL uses Microsoft.Extensions.Logging (MEL) as logging infrastructure. However, you can use logging adapters to enable logging with other logging libraries.

Library references

Setup loggers

All the types that implements IWikiClientLoggable interface can be associated with an ILogger instance via Logger property. To enable logging, simply assign it with your logger.

ILoggerFactory loggerFactory = ...;    // Set up your LoggerFactory here.
var client = new WikiClient { Logger = loggerFactory.CreateLogger("WikiClient") };
var site = new WikiSite("https://en.wikipedia.org/w/api.php") { Logger = loggerFactory.CreateLogger("enwp") };

Scopes

Note that this library uses MEL logging scope to provide extra contextual information, such as the site and the page where the operation is performed. If you need such information, please be careful to use ILogger implementations that support scope (i.e. has properly implemented ILogger.BeginScope<T>(T) method). For example, you might want to set includeScopes parameter to true when constructing ConsoleLogger, or using loggerFactory.AddConsole.

If you are implementing your own scope-enabled ILogger types, please take a look at the source code of BeginScope in MS's ConsoleLogger, or WCL's TestOutputLogger.

The scope-enabled loggers may output some extra information about the context, such as

Debug: WikiClientLibrary.Sites.WikiSite
        -> Wikipedia -> WikiPage{Wikipedia:Sandbox}::UpdateContentAsync()
       Edit: Wikipedia:Sandbox#51235807: Waiting for delay 00:00:05.
Debug: WikiClientLibrary.Sites.WikiSite
        -> Wikipedia -> WikiPage{Wikipedia:Sandbox}::UpdateContentAsync() -> Wikipedia -> ::FetchTokenAsyncCore(csrf)
       Sending request 452849D100000008, SuppressAccountAssertion=True
Trace: WikiClientLibrary.Client.WikiClient
        -> Wikipedia -> WikiPage{Wikipedia:Sandbox}::UpdateContentAsync() -> Wikipedia -> ::FetchTokenAsyncCore(csrf) -> WikiClient#17363659 -> ::InvokeAsync(452849D100000008)
       Initiate request to: https://test2.wikipedia.org/w/api.php.
Trace: WikiClientLibrary.Client.WikiClient
        -> Wikipedia -> WikiPage{Wikipedia:Sandbox}::UpdateContentAsync() -> Wikipedia -> ::FetchTokenAsyncCore(csrf) -> WikiClient#17363659 -> ::InvokeAsync(452849D100000008)
       HTTP 200, elapsed: 00:00:00.3663925
Debug: WikiClientLibrary.Sites.WikiSite
        -> Wikipedia -> WikiPage{Wikipedia:Sandbox}::UpdateContentAsync()
       Sending request 452849D100000007, SuppressAccountAssertion=False
Trace: WikiClientLibrary.Client.WikiClient
        -> Wikipedia -> WikiPage{Wikipedia:Sandbox}::UpdateContentAsync() -> WikiClient#17363659 -> ::InvokeAsync(452849D100000007)
       Initiate request to: https://test2.wikipedia.org/w/api.php.
Trace: WikiClientLibrary.Client.WikiClient
        -> Wikipedia -> WikiPage{Wikipedia:Sandbox}::UpdateContentAsync() -> WikiClient#17363659 -> ::InvokeAsync(452849D100000007)
       HTTP 200, elapsed: 00:00:00.5573600
Information: WikiClientLibrary.Sites.WikiSite
              -> Wikipedia -> WikiPage{Wikipedia:Sandbox}::UpdateContentAsync()
             Edited page. New revid=329827.