Skip to content

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

Merged
merged 2 commits into from
Mar 17, 2017

Conversation

chrisdunelm
Copy link
Contributor

And provide a MemoryLogger (for testing); and a simple ConsoleLogger.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Mar 17, 2017
@chrisdunelm chrisdunelm force-pushed the rrlogging branch 3 times, most recently from fdc2ace to 6740f63 Compare March 17, 2017 08:00
And provide a MemoryLogger (for testing); and a simple ConsoleLogger.
Copy link
Collaborator

@jskeet jskeet left a 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.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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.

/// <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.

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.

This comment was marked as spam.

Copy link
Contributor Author

@chrisdunelm chrisdunelm left a 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.

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.

/// <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.

public ILogger ForType<T>() => ForType(typeof(T));

/// <inheritdoc/>
public ILogger ForType(Type type)

This comment was marked as spam.

@chrisdunelm chrisdunelm merged commit e111a5a into googleapis:master Mar 17, 2017
@chrisdunelm chrisdunelm deleted the rrlogging branch March 17, 2017 09:28
@riyadparvez
Copy link

How do I enable logging for the client library?

@chrisdunelm
Copy link
Contributor Author

@riyadparvez Use Google.ApplicationContext.RegisterLogger(...)

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.

@riyadparvez
Copy link

riyadparvez commented Oct 17, 2017

Thanks for your prompt reply.
This is how I'm initializing the service:

        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 GmailService object.

@chrisdunelm
Copy link
Contributor Author

Google.ApplicationContext.RegisterLogger must be called very early in your code's execution.

ConfigurableMessageHandler loads the logger the first time it is used, and will not re-load the logger if you subsequently register a logger.

Call RegisterLogger before any code references Google.Apis.*.

@riyadparvez
Copy link

Now it works. Thanks for the fix.

@chrisdunelm
Copy link
Contributor Author

Great :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants