[CSHARP-2829] Add logging to the driver Created: 31/Oct/19  Updated: 18/Apr/22  Resolved: 29/Apr/20

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Epic Priority: Major - P3
Reporter: Esha Bhargava Assignee: Unassigned
Resolution: Won't Do Votes: 0
Labels: FY21Q1, driver-planning-backlog
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2020-05-12-12-33-50-980.png    
Issue Links:
Depends
is depended on by DRIVERS-1204 Easier debugging with standardized lo... Implementing
Duplicate
duplicates CSHARP-3893 Integrate with .NET Logging API Closed

 Description   
Epic Summary

Summary

Currently the driver has no way of logging. This has been a pain point when helping customers debug, especially when there is no overt crash with a stack trace to provide some additional information. This epic tracks the effort needed to add logging to the driver so that we can request that customers turn on logging to help them debug their issues.

Motivation

Cast of Characters

Lead:
Author:

Documentation

[Scope Document|]
[Technical Design Document|]



 Comments   
Comment by Esha Bhargava [ 29/Apr/20 ]

We'll do this as a part of the .Net ticket generated from DRIVERS-1204

Comment by Robert Stam [ 27/Apr/20 ]

There already is some support in the driver for logging. You can register an event subscriber that will log all events raised by the driver.

To log all events raised by the driver configure the MongoClientSettings in code to add a cluster configurator:

var settings = MongoClientSettings.FromConnectionString("mongodb://localhost");
settings.ClusterConfigurator = EventLoggingConfigurator;
var client = new MongoClient(settings);

which uses the following helper method:

private static void EventLoggingConfigurator(ClusterBuilder builder)
{
    var logFilename = @"C:\projects\TestCSharp2829\log.txt";
    File.Delete(logFilename); // more likely you will want to rotate the logs
 
    var traceSource = new TraceSource("CSHARPDRIVER", SourceLevels.All);
    traceSource.Listeners.Clear(); // remove the default listener
    var fileStream = new FileStream(logFilename, FileMode.Append);
    var listener = new TextWriterTraceListener(fileStream);
    listener.TraceOutputOptions = TraceOptions.DateTime;
    traceSource.Listeners.Add(listener);
    var subscriber = new TraceSourceEventSubscriber(traceSource);
    builder.Subscribe(subscriber);
}

This isn't particularly easy to use, and it also only logs Events.

We probably want to decouple logging from Events at some point.

Generated at Wed Feb 07 21:43:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.