Wenn Sie sich mit anderen Nutzern über unsere Produkte austauschen und Feedback geben möchten, können Sie dem offiziellen Google Ads-Discord-Kanal auf dem Server der Google Advertising and Measurement Community beitreten.
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Die .NET-Clientbibliothek verwendet intern gRPC-Interceptors für Logging-Zwecke. Sie können aber auch eigene benutzerdefinierte gRPC-Interceptors hinzufügen. Mit dieser Funktion können Sie Messwerte in einem benutzerdefinierten Log-Aggregator protokollieren oder Google Ads API-Anfragen transformieren.
Wenn Sie einen benutzerdefinierten gRPC-Interceptor hinzufügen möchten, implementieren Sie eine Klasse, die die Klasse Grpc.Core.Interceptors.Interceptor erweitert, und hängen Sie sie mit der Methode AddInterceptor an Ihren GoogleAdsClient an.
Das Abfangen von Streaming- und Nicht-Streaming-gRPC-Anfragen erfordert das Überschreiben von zwei verschiedenen Methoden der Klasse Interceptor: AsyncUnaryCall bzw. AsyncServerStreamingCall.
Der Google.Ads.Gax.Interceptors-Namespace enthält Hilfsklassen, die in benutzerdefinierten Interceptoren verwendet werden können, um die Ausnahmebehandlung zu vereinfachen.
Im folgenden Codebeispiel werden sowohl ein Streaming- als auch ein Nicht-Streaming-gRPC-Aufruf-Interceptor implementiert:
// Copyright 2024 Google LLC//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.usingCommandLine;usingGoogle.Api.Gax;usingGoogle.Ads.Gax.Examples;usingGoogle.Ads.Gax.Interceptors;usingGoogle.Ads.GoogleAds.Lib;usingGoogle.Ads.GoogleAds.V21.Errors;usingGoogle.Ads.GoogleAds.V21.Services;usingGrpc.Core;usingGrpc.Core.Interceptors;usingSystem;usingSystem.Threading.Tasks;namespaceGoogle.Ads.GoogleAds.Examples.V21{/// <summary>/// This code example shows how to add a custom gRPC interceptor./// </summary>publicclassAddCustomGrpcInterceptor:ExampleBase{/// <summary>/// Command line options for running the <see cref="AddCustomGrpcInterceptor"/> example./// </summary>publicclassOptions:OptionsBase{/// <summary>/// The Google Ads customer ID for which the call is made./// </summary>[Option("customerId",Required=true,HelpText="The Google Ads customer ID for which the call is made.")]publiclongCustomerId{get;set;}}/// <summary>/// Main method, to run this code example as a standalone application./// </summary>/// <param name="args">The command line arguments.</param>publicstaticvoidMain(string[]args){Optionsoptions=ExampleUtilities.ParseCommandLine<Options>(args);AddCustomGrpcInterceptorcodeExample=newAddCustomGrpcInterceptor();Console.WriteLine(codeExample.Description);GoogleAdsClientclient=newGoogleAdsClient();// Add a custom interceptor.client.AddInterceptor(newCustomInterceptor());codeExample.Run(client,options.CustomerId);}/// <summary>/// Returns a description about the code example./// </summary>publicoverridestringDescription=>
"This code example shows how to add a custom gRPC interceptor.";/// <summary>/// Runs the code example./// </summary>/// <param name="client">The Google Ads client.</param>/// <param name="customerId">The Google Ads customer ID for which the call is made.</param>publicvoidRun(GoogleAdsClientclient,longcustomerId){// Get the GoogleAdsService.GoogleAdsServiceClientgoogleAdsService=client.GetService(Services.V21.GoogleAdsService);// Create a query that will retrieve all campaigns, just to demonstrate usage of the// custom interceptor.stringquery=@"SELECTcampaign.id,FROMcampaignORDERBYcampaign.id";try{// Issue a streaming search request; we don't need to do anything with the response// here, we just want to demonstrate usage of the interceptor.googleAdsService.SearchStream(customerId.ToString(),query,delegate(SearchGoogleAdsStreamResponseresp){});}catch(GoogleAdsExceptione){Console.WriteLine("Failure:");Console.WriteLine($"Message: {e.Message}");Console.WriteLine($"Failure: {e.Failure}");Console.WriteLine($"Request ID: {e.RequestId}");throw;}try{// Issue a non-streaming call.PagedEnumerable<SearchGoogleAdsResponse,GoogleAdsRow>response=googleAdsService.Search(customerId.ToString(),query);foreach(GoogleAdsRowgoogleAdsRowinresponse){// The response for Search is lazy, meaning that the actual gRPC call will be// sent only when the response is actually accessed; to demonstrate usage of// the interceptor, then, we need to ensure the call is sent by looping// through the response results.Console.WriteLine("Campaign with ID {0} was found.",googleAdsRow.Campaign.Id,googleAdsRow.Campaign.Name);}}catch(GoogleAdsExceptione){Console.WriteLine("Failure:");Console.WriteLine($"Message: {e.Message}");Console.WriteLine($"Failure: {e.Failure}");Console.WriteLine($"Request ID: {e.RequestId}");throw;}}}/// <summary>/// A custom interceptor for both streaming and non-streaming gRPC calls./// </summary>internalclassCustomInterceptor:Interceptor{publicoverrideAsyncUnaryCall<TResponse>AsyncUnaryCall<TRequest,TResponse>(TRequestrequest,ClientInterceptorContext<TRequest,TResponse>context,AsyncUnaryCallContinuation<TRequest,TResponse>continuationCallback){AsyncUnaryCall<TResponse>call=continuationCallback(request,context);Action<Task<TResponse>>callback=delegate(Task<TResponse>oldTask){Console.WriteLine($"Intercepted a non-streaming call to {context.Method.Name}");};returnUnaryRpcInterceptor.Intercept(call,callback);}publicoverrideAsyncServerStreamingCall<TResponse>AsyncServerStreamingCall<TRequest,TResponse>(TRequestrequest,ClientInterceptorContext<TRequest,TResponse>context,AsyncServerStreamingCallContinuation<TRequest,TResponse>continuation){AsyncServerStreamingCall<TResponse>call=continuation(request,context);StreamingRpcInterceptor<TResponse>responseStream=null;responseStream=newStreamingRpcInterceptor<TResponse>(call.ResponseStream,delegate(TResponseresponse,AggregateExceptionrpcException){Console.WriteLine($"Intercepted a streaming call to {context.Method.Name}");});returnnewAsyncServerStreamingCall<TResponse>(responseStream,call.ResponseHeadersAsync,call.GetStatus,call.GetTrailers,call.Dispose);}}}
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Benötigte Informationen nicht gefunden","missingTheInformationINeed","thumb-down"],["Zu umständlich/zu viele Schritte","tooComplicatedTooManySteps","thumb-down"],["Nicht mehr aktuell","outOfDate","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Problem mit Beispielen/Code","samplesCodeIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-05 (UTC)."],[[["\u003cp\u003eStarting with version 19.0.0, the Google Ads API client library for .NET will no longer support .NET 5.0, requiring users to upgrade before the end of 2024 for continued functionality.\u003c/p\u003e\n"],["\u003cp\u003eThe .NET client library allows adding custom gRPC interceptors for tasks like logging metrics to custom aggregators or transforming Google Ads API requests.\u003c/p\u003e\n"],["\u003cp\u003eCustom interceptors are implemented by extending the \u003ccode\u003eGrpc.Core.Interceptors.Interceptor\u003c/code\u003e class and attached to the \u003ccode\u003eGoogleAdsClient\u003c/code\u003e using the \u003ccode\u003eAddInterceptor\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eInterception of streaming and non-streaming calls requires overriding the \u003ccode\u003eAsyncServerStreamingCall\u003c/code\u003e and \u003ccode\u003eAsyncUnaryCall\u003c/code\u003e methods, respectively, within the custom interceptor class.\u003c/p\u003e\n"]]],[],null,["# Custom gRPC interceptors\n\nThe .NET client library internally uses gRPC [interceptors](//grpc.io/docs/guides/interceptors/) for logging purposes, but it also lets\nyou add your own custom gRPC interceptors. This feature can be used for logging\nmetrics to a custom log aggregator or to transform Google Ads API requests.\n\nTo add a custom gRPC interceptor, implement a class extending the\n`Grpc.Core.Interceptors.Interceptor` class and attach it to your\n`GoogleAdsClient` with the `AddInterceptor` method.\n\nNote that intercepting streaming and non-streaming gRPC requests require\noverriding two different methods of the `Interceptor` class:\n`AsyncUnaryCall` and `AsyncServerStreamingCall`, respectively.\n\nThe `Google.Ads.Gax.Interceptors` namespace provides utility classes that can be\nused in custom interceptors to facilitate exception handling.\n\nThe following code example implements both a streaming and a non-streaming gRPC\ncall interceptor:\n\n\n```objective-c\n// Copyright 2024 Google LLC\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\nusing CommandLine;\nusing Google.Api.Gax;\nusing Google.Ads.Gax.Examples;\nusing Google.Ads.Gax.Interceptors;\nusing Google.Ads.GoogleAds.Lib;\nusing Google.Ads.GoogleAds.V21.Errors;\nusing Google.Ads.GoogleAds.V21.Services;\nusing Grpc.Core;\nusing Grpc.Core.Interceptors;\nusing System;\n\nusing System.Threading.Tasks;\n\nnamespace Google.Ads.GoogleAds.Examples.V21\n{\n /// \u003csummary\u003e\n /// This code example shows how to add a custom gRPC interceptor.\n /// \u003c/summary\u003e\n public class AddCustomGrpcInterceptor : ExampleBase\n {\n /// \u003csummary\u003e\n /// Command line options for running the \u003csee cref=\"AddCustomGrpcInterceptor\"/\u003e example.\n /// \u003c/summary\u003e\n public class Options : OptionsBase\n {\n /// \u003csummary\u003e\n /// The Google Ads customer ID for which the call is made.\n /// \u003c/summary\u003e\n [Option(\"customerId\", Required = true, HelpText =\n \"The Google Ads customer ID for which the call is made.\")]\n public long CustomerId { get; set; }\n }\n\n /// \u003csummary\u003e\n /// Main method, to run this code example as a standalone application.\n /// \u003c/summary\u003e\n /// \u003cparam name=\"args\"\u003eThe command line arguments.\u003c/param\u003e\n public static void Main(string[] args)\n {\n Options options = ExampleUtilities.ParseCommandLine\u003cOptions\u003e(args);\n\n AddCustomGrpcInterceptor codeExample = new AddCustomGrpcInterceptor();\n Console.WriteLine(codeExample.Description);\n GoogleAdsClient client = new GoogleAdsClient();\n // Add a custom interceptor.\n client.AddInterceptor(new CustomInterceptor());\n codeExample.Run(client,\n options.CustomerId);\n }\n\n /// \u003csummary\u003e\n /// Returns a description about the code example.\n /// \u003c/summary\u003e\n public override string Description =\u003e\n \"This code example shows how to add a custom gRPC interceptor.\";\n\n /// \u003csummary\u003e\n /// Runs the code example.\n /// \u003c/summary\u003e\n /// \u003cparam name=\"client\"\u003eThe Google Ads client.\u003c/param\u003e\n /// \u003cparam name=\"customerId\"\u003eThe Google Ads customer ID for which the call is made.\u003c/param\u003e\n public void Run(GoogleAdsClient client, long customerId)\n {\n // Get the GoogleAdsService.\n GoogleAdsServiceClient googleAdsService = client.GetService(\n Services.V21.GoogleAdsService);\n\n // Create a query that will retrieve all campaigns, just to demonstrate usage of the\n // custom interceptor.\n string query = @\"SELECT\n campaign.id,\n FROM campaign\n ORDER BY campaign.id\";\n\n try\n {\n // Issue a streaming search request; we don't need to do anything with the response\n // here, we just want to demonstrate usage of the interceptor.\n googleAdsService.SearchStream(customerId.ToString(), query,\n delegate (SearchGoogleAdsStreamResponse resp){}\n );\n }\n catch (GoogleAdsException e)\n {\n Console.WriteLine(\"Failure:\");\n Console.WriteLine($\"Message: {e.Message}\");\n Console.WriteLine($\"Failure: {e.Failure}\");\n Console.WriteLine($\"Request ID: {e.RequestId}\");\n throw;\n }\n try\n {\n // Issue a non-streaming call.\n PagedEnumerable\u003cSearchGoogleAdsResponse, GoogleAdsRow\u003e response =\n googleAdsService.Search(customerId.ToString(), query);\n foreach (GoogleAdsRow googleAdsRow in response)\n {\n // The response for Search is lazy, meaning that the actual gRPC call will be\n // sent only when the response is actually accessed; to demonstrate usage of\n // the interceptor, then, we need to ensure the call is sent by looping\n // through the response results.\n Console.WriteLine(\"Campaign with ID {0} was found.\",\n googleAdsRow.Campaign.Id, googleAdsRow.Campaign.Name);\n }\n }\n catch (GoogleAdsException e)\n {\n Console.WriteLine(\"Failure:\");\n Console.WriteLine($\"Message: {e.Message}\");\n Console.WriteLine($\"Failure: {e.Failure}\");\n Console.WriteLine($\"Request ID: {e.RequestId}\");\n throw;\n }\n }\n }\n\n /// \u003csummary\u003e\n /// A custom interceptor for both streaming and non-streaming gRPC calls.\n /// \u003c/summary\u003e\n internal class CustomInterceptor : Interceptor\n {\n\n public override AsyncUnaryCall\u003cTResponse\u003e AsyncUnaryCall\u003cTRequest, TResponse\u003e(\n TRequest request, ClientInterceptorContext\u003cTRequest, TResponse\u003e context,\n AsyncUnaryCallContinuation\u003cTRequest, TResponse\u003e continuationCallback)\n {\n AsyncUnaryCall\u003cTResponse\u003e call = continuationCallback(request, context);\n\n Action\u003cTask\u003cTResponse\u003e\u003e callback = delegate (Task\u003cTResponse\u003e oldTask)\n {\n Console.WriteLine($\"Intercepted a non-streaming call to {context.Method.Name}\");\n };\n\n return UnaryRpcInterceptor.Intercept(call, callback);\n }\n\n public override AsyncServerStreamingCall\u003cTResponse\u003e AsyncServerStreamingCall\u003cTRequest, TResponse\u003e(\n TRequest request, ClientInterceptorContext\u003cTRequest, TResponse\u003e context,\n AsyncServerStreamingCallContinuation\u003cTRequest, TResponse\u003e continuation)\n {\n AsyncServerStreamingCall\u003cTResponse\u003e call = continuation(request, context);\n StreamingRpcInterceptor\u003cTResponse\u003e responseStream = null;\n\n responseStream = new StreamingRpcInterceptor\u003cTResponse\u003e(call.ResponseStream,\n delegate (TResponse response, AggregateException rpcException)\n {\n Console.WriteLine($\"Intercepted a streaming call to {context.Method.Name}\");\n });\n\n return new AsyncServerStreamingCall\u003cTResponse\u003e(\n responseStream,\n call.ResponseHeadersAsync,\n call.GetStatus,\n call.GetTrailers,\n call.Dispose\n );\n }\n }\n}\nhttps://github.com/googleads/google-ads-dotnet/blob/ada966e1983b655e82172b6c3e7d9b091b522377/Google.Ads.GoogleAds/examples/AdvancedOperations/AddCustomGrpcInterceptor.cs\n \n```\n\n\u003cbr /\u003e"]]