Logging in SDK
The Graph SDK uses a custom logger named IGraphLogger which allows custom subscribers for log events. These events can be observed and logged as required by the bot developers.
You need to create an IObserver
for log events (the LogEvent class).
private class LogObserver : IObserver<LogEvent>
{
private readonly LogEventFormatter formatter = new LogEventFormatter();
/// <summary>
/// Provides the observer with new data.
/// </summary>
/// <param name="logEvent">The current notification information.</param>
public void OnNext(LogEvent logEvent)
{
// Log event.
// Event Severity: logEvent.Level
// Http trace: logEvent.EventType == LogEventType.HttpTrace
// Log trace: logEvent.EventType == LogEventType.Trace
var logString = this.formatter.Format(logEvent);
MyLogger.Log(logEvent.Level, logString)
}
/// <summary>
/// Notifies the observer that the provider has experienced an error condition.
/// </summary>
/// <param name="error">An object that provides additional information about the error.</param>
public void OnError(Exception error)
{
// Error occurred with the logger, not with the SDK.
}
/// <summary>
/// Notifies the observer that the provider has finished sending push-based notifications.
/// </summary>
public void OnCompleted()
{
// Graph Logger has completed logging (shutdown).
}
}
After a class is ready, you need to subscribe to the log events.
ICommunicationsClient client; // Substitute this with the instance of the SDK.
var observer = new LogObserver();
var disposableSubsription = client.GraphLogger.Subscribe(observer);
Note
The disposableSubscription
object should be persisted, otherwise logging will not work once it is garbage collected.