-
Notifications
You must be signed in to change notification settings - Fork 534
Provide request/response logging #935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
fdc2ace
to
6740f63
Compare
And provide a MemoryLogger (for testing); and a simple ConsoleLogger.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally fine - a few nits. Will definitely make life nicer.
We may well want a TextWriterLogger some time in the future, but we can add that later.
private ILogger _instanceLogger = Logger; | ||
|
||
/// <summary> | ||
/// For testing only |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
public ILogger ForType<T>() => ForType(typeof(T)); | ||
|
||
/// <inheritdoc/> | ||
public ILogger ForType(Type type) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
/// <summary> | ||
/// The type from which entries are being logged. May be <c>null</c>. | ||
/// </summary> | ||
public Type LoggerForType { get; } |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
public abstract class BaseLogger : ILogger | ||
{ | ||
// Matches gRPC datetime log format | ||
private const string DateTimeFormatString = "MMdd HH:mm:ss.ffffff"; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, a TextWriterLogger may well be useful later, as might be a DebugLogger and a TraceLogger.
As you say, these can be added later.
private ILogger _instanceLogger = Logger; | ||
|
||
/// <summary> | ||
/// For testing only |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
public abstract class BaseLogger : ILogger | ||
{ | ||
// Matches gRPC datetime log format | ||
private const string DateTimeFormatString = "MMdd HH:mm:ss.ffffff"; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
/// <summary> | ||
/// The type from which entries are being logged. May be <c>null</c>. | ||
/// </summary> | ||
public Type LoggerForType { get; } |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
public ILogger ForType<T>() => ForType(typeof(T)); | ||
|
||
/// <inheritdoc/> | ||
public ILogger ForType(Type type) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
How do I enable logging for the client library? |
@riyadparvez Use Here's an example: Google.ApplicationContext.RegisterLogger(new Google.Apis.Logging.ConsoleLogger(Google.Apis.Logging.LogLevel.All));
var service = new Google.Apis.Storage.v1.StorageService();
service.HttpClient.MessageHandler.LogEvents |= Google.Apis.Http.ConfigurableMessageHandler.LogEventType.RequestBody;
service.Buckets.List("myProject").Execute(); This code does not successfully call into Google Storage, as there's no authentication; but does show logging to the console. Other loggers are also available. |
Thanks for your prompt reply. private GmailService CreateService(string clientId, string clientSecret)
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/gmail-credentials");
var credentials = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = clientId,
ClientSecret = clientSecret,
},
Constants.Scopes,
UserId,
CancellationToken.None,
new FileDataStore(credPath))
.Result;
Utils.Logger.Debug($"Credential file saved to: {credPath}");
// We're building the service object, but there's no guarantee that
// we've logged in or anything
var service = new GmailService(new BaseClientService.Initializer
{
HttpClientInitializer = credentials,
ApplicationName = "GmailClient",
})
{
HttpClient =
{
MessageHandler =
{
IsLoggingEnabled = true,
LogEvents = ConfigurableMessageHandler.LogEventType.ResponseStatus,
},
MaxResponseContentBufferSize = Int32.MaxValue ,
Timeout = new TimeSpan(0, 0, 200),
}
};
service.HttpClient.MessageHandler.AddExceptionHandler(new HttpExceptionHandler());
service.HttpClient.MessageHandler.AddUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler());
Google.ApplicationContext.RegisterLogger(new Google.Apis.Logging.ConsoleLogger(Google.Apis.Logging.LogLevel.All));
//Google.ApplicationContext.RegisterLogger(new Google.Apis.Logging.MemoryLogger(Google.Apis.Logging.LogLevel.All));
service.HttpClient.MessageHandler.LogEvents |= Google.Apis.Http.ConfigurableMessageHandler.LogEventType.RequestBody;
service.HttpClient.MessageHandler.LogEvents |= Google.Apis.Http.ConfigurableMessageHandler.LogEventType.ResponseBody;
service.HttpClient.MessageHandler.LogEvents |= Google.Apis.Http.ConfigurableMessageHandler.LogEventType.ResponseStatus;
return service;
} Unfortunately I still don't see anything on console when I execute API call through the |
Call |
Now it works. Thanks for the fix. |
Great :) |
And provide a MemoryLogger (for testing); and a simple ConsoleLogger.