The .NET client library logs requests, responses, and summary messages made to
the Google Ads API. The logs may be written to a default logger in the .NET
library, or a custom
TraceListener
.
Configuration
You can enable logging by adding the following line in your Main
method before
making any API calls.
// Detailed logs.
TraceUtilities.Configure(TraceUtilities.DETAILED_REQUEST_LOGS_SOURCE,
"C:\\logs\\details.log", System.Diagnostics.SourceLevels.All);
// Summary logs.
TraceUtilities.Configure(TraceUtilities.SUMMARY_REQUEST_LOGS_SOURCE,
"C:\\logs\\details.log", System.Diagnostics.SourceLevels.All);
Configuration using App.config (legacy)
If your app builds for a .NET Framework target, you can load the logging
configuration from your app's App.config
or Web.config
file. This is a
legacy .NET functionality that is not supported for apps built for .NET Core
targets.
To use this feature, you need to add the following changes to your configuration file:
Add the following snippet under the
<configuration>
section.<system.diagnostics> <sources> <source name="GoogleAds.DeprecationMessages" switchName="GoogleAds.DeprecationMessages" switchType="System.Diagnostics.SourceSwitch"> <listeners> <add name="myListener" type="System.Diagnostics.EventLogTraceListener" initializeData="Application"/> </listeners> </source> <source name="GoogleAds.DetailedRequestLogs" switchName="GoogleAds.DetailedRequestLogs" switchType="System.Diagnostics.SourceSwitch"> <listeners> <add name="detailedRequestLogListener" type="System.Diagnostics.ConsoleTraceListener" initializeData="true"/> <!-- Use the following to log to file. Modify the initializeData attribute to control the path to the detailed request log file. --> <!-- <add name="detailedRequestLogListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Logs\detailed_logs.log"/> <remove name="Default"/> --> </listeners> </source> <source name="GoogleAds.SummaryRequestLogs" switchName="GoogleAds.SummaryRequestLogs" switchType="System.Diagnostics.SourceSwitch"> <listeners> <add name="summaryRequestLogListener" type="System.Diagnostics.ConsoleTraceListener" initializeData="true"/> <!-- Use the following to log to file. Modify the initializeData attribute to control the path to the summary request log file. --> <!-- <add name="summaryRequestLogListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Logs\summary_logs.log"/> --> <remove name="Default"/> </listeners> </source> </sources> <switches> <!-- Use this trace switch to control the deprecation trace messages written by Ads* .NET libraries. The default is level is set to Warning. To disable all messages, set this value to Off. See msdn.microsoft.com/en-us/library/system.diagnostics.sourcelevels.aspx for all possible values this key can take. --> <add name="GoogleAds.DeprecationMessages" value="Warning"/> <!-- Use this trace switch to control the detailed request logs written by Ads* .NET libraries. The default level is set to Off. Logs are generated at both the Error and Information levels. --> <add name="GoogleAds.DetailedRequestLogs" value="Off"/> <!-- Use this trace switch to control the summary request logs written by Ads* .NET libraries. The default level is set to Off. Logs are generated at both the Error and Information levels. --> <add name="GoogleAds.SummaryRequestLogs" value="Off"/> </switches> <trace autoflush="true"/> </system.diagnostics>
Add the following snippet under the
<configSections>
section.<section name="system.diagnostics" type="System.Diagnostics.SystemDiagnosticsSection"/>
Your
App.config
then looks like this:<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="GoogleAdsApi" type="System.Configuration.DictionarySectionHandler"/> <section name="system.diagnostics" type="System.Diagnostics.SystemDiagnosticsSection"/> </configSections> <GoogleAdsApi> <!-- Google Ads API settings. --> </GoogleAdsApi> <system.diagnostics> <!-- Logging settings. --> </system.diagnostics> </configuration>
Log levels
The library logs different types of events to different log levels. On a
successful API response, the summary is logged at INFO
, and the full
request and responses are logged at DEBUG
. On a request that results in
an API error, the summary message is logged at WARN
, and the full request
and response is logged at INFO
.
Partial failures are logged at DEBUG
.
Advanced logging
In most cases, the logs generated by the client library provide sufficient details to troubleshoot your issues. When reaching out to the support forum/aliases, you can either provide the logs (which redacts sensitive information by default), or just share the request ID (which is logged as part of the response log).
If you prefer capturing the request ID yourself, provide a
GoogleResponseMetadata
object to capture the request ID:
GoogleAdsResponseMetadata metadata = new GoogleAdsResponseMetadata(client.Config);
// Add the campaigns.
MutateCampaignsResponse retVal = campaignService.MutateCampaigns(
customerId.ToString(), operations.ToArray(), metadata.CallSettings);
Console.WriteLine(metadata.RequestId);
If the API log doesn't give you enough details, enable more low level logging at the gRPC level. Keep in mind that the output can be voluminous. The gRPC logs are written to stderr, but you can attach your own logger as shown below. Supported environment variables.
Environment.SetEnvironmentVariable("GRPC_VERBOSITY", "DEBUG");
Environment.SetEnvironmentVariable("GRPC_TRACE", "http");
GrpcEnvironment.SetLogger(new ConsoleLogger());