public async Task LogAsync() { Mock <ReportErrorsServiceClient> mockClient = new Mock <ReportErrorsServiceClient>(); mockClient.Setup(client => client.ReportErrorEventAsync(ProjectName, IsComplexContext(), null)) .Returns(Task.FromResult(new ReportErrorEventResponse())); ErrorReportingExceptionLogger logger = GetLogger(mockClient.Object); await logger.LogAsync(CreateComplexContext(), CancellationToken.None); mockClient.VerifyAll(); }
public void Log_Simple() { Mock <ReportErrorsServiceClient> mockClient = new Mock <ReportErrorsServiceClient>(); mockClient.Setup(client => client.ReportErrorEvent(ProjectName, IsSimpleContext(), null)); ErrorReportingExceptionLogger logger = GetLogger(mockClient.Object); logger.Log(SimpleContext); mockClient.VerifyAll(); }
public void Report_Simple() { var mockClient = new Mock <ReportErrorsServiceClient>(); var logger = ErrorReportingExceptionLogger.Create( Task.FromResult(mockClient.Object), _projectId, _serviceName, _version); var context = new DefaultHttpContext(); context.Request.Host = new HostString(_googleHost); mockClient.Setup(client => client.ReportErrorEvent(s_projectName, IsSimpleContext(), null)); logger.Log(context, new Exception(s_exceptionMessage)); mockClient.VerifyAll(); }
public async Task Report_Complex() { var mockConsumer = new Mock <IConsumer <ReportedErrorEvent> >(); var logger = new ErrorReportingExceptionLogger( mockConsumer.Object, ErrorReportingMatching.ServiceName, ErrorReportingMatching.VersionName); bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); mockConsumer.Setup(c => c.ReceiveAsync(s_matcher.IsComplexContext(), default(CancellationToken))) .Returns(CommonUtils.CompletedTask); await logger.LogAsync(CreateComplexContext(), s_matcher.CreateException()); mockConsumer.VerifyAll(); }
public async Task LogAsync() { var mockContextLogger = new Mock <IContextExceptionLogger>(); mockContextLogger.Setup(cl => cl.LogAsync( It.IsAny <Exception>(), It.IsAny <IContextWrapper>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(true)); var logger = new ErrorReportingExceptionLogger(mockContextLogger.Object); await logger.LogAsync(s_simpleContext, CancellationToken.None); mockContextLogger.Verify(cl => cl.LogAsync( s_simpleContext.Exception, It.IsAny <ExceptionLoggerContextWrapper>(), It.IsAny <CancellationToken>())); }
public static void Register(HttpConfiguration config) { // To enable Google Cloud Stackdrive Logging and Error Reporting // while running on your local machine edit Web.config and uncomment // the <projectId> value under the <log4net> section. Ensure that // the <projectId> is set to a valid Google Cloud Project Id. // Otherwise, logging will only occur when deployed to GCP. string projectId = Google.Api.Gax.Platform.Instance().GceDetails?.ProjectId ?? GetProjectIdFromConfig(); if (!string.IsNullOrEmpty(projectId)) { var serviceName = ConfigurationManager.AppSettings["google_error_reporting:serviceName"]; var version = ConfigurationManager.AppSettings["google_error_reporting:version"]; // Add a catch all to log all uncaught exceptions to Stackdriver Error Reporting. config.Services.Add( typeof(IExceptionLogger), ErrorReportingExceptionLogger.Create(projectId, serviceName, version)); // Retrieve a logger for this context. ILog log = LogManager.GetLogger(typeof(WebApiConfig)); // Log confirmation of set-up to Google Stackdriver Error Reporting. log.Info("Stackdriver Error Reporting enabled: https://console.cloud.google.com/errors/"); } else { // Retrieve a logger for this context. ILog log = LogManager.GetLogger(typeof(WebApiConfig)); // Log failure of set-up to Google Stackdriver Error Reporting. log.Warn("Stackdriver Error Reporting not enabled. ProjectId missing from configuration."); } // Web API configuration and services // Configure Web API to use only bearer token authentication. config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new {id = RouteParameter.Optional} ); }
public async Task Report_Simple() { var mockConsumer = new Mock <IConsumer <ReportedErrorEvent> >(); var logger = new ErrorReportingExceptionLogger( mockConsumer.Object, ErrorReportingMatching.ServiceName, ErrorReportingMatching.VersionName); var context = new DefaultHttpContext(); context.Request.Host = new HostString(s_googleUri); context.Response.StatusCode = 0; mockConsumer.Setup(c => c.ReceiveAsync(s_matcher.IsSimpleContext(), default(CancellationToken))) .Returns(CommonUtils.CompletedTask); await logger.LogAsync(context, new Exception(ErrorReportingMatching.ExceptionMessage)); mockConsumer.VerifyAll(); }
public static void Register(HttpConfiguration config) { var emptyDictionary = new HttpRouteValueDictionary(); // Add our one HttpMessageHandler to the root path. config.Routes.MapHttpRoute("index", "", emptyDictionary, emptyDictionary, new HelloWorldHandler()); // Add a catch all for the uncaught exceptions. string projectId = "pacific-wind"; string serviceName = "myservice"; string version = "version1"; // Add a catch all for the uncaught exceptions. config.Services.Add(typeof(IExceptionLogger), ErrorReportingExceptionLogger.Create(projectId, serviceName, version)); }
// Sample: RegisterExceptionLoggerWebApi private static void Register(HttpConfiguration config) { // Add a catch all for the uncaught exceptions. // Replace ProjectId with your Google Cloud Project ID. // Replace Service with a name or identifier for the service. // Replace Version with a version for the service. config.Services.Add(typeof(System.Web.Http.ExceptionHandling.IExceptionLogger), ErrorReportingExceptionLogger.Create(ProjectId, Service, Version)); // Any other actions your app might need. // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); }
public static void Register(HttpConfiguration config) { // Web API configuration and services // Configure Web API to use only bearer token authentication. config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); // Enable Error Reporting config.Services.Add(typeof(IExceptionLogger), ErrorReportingExceptionLogger.Create("cloud-sharp-work", "service", "version")); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
public static void Register(HttpConfiguration config) { // Web API configuration and services // Add a catch all for the uncaught exceptions. // Replace ProjectId with your Google Cloud Project ID. // Replace Service with a name or identifier for the service. // Replace Version with a version for the service. #if DEBUG var credentialKey = ConfigurationManager.AppSettings["GOOGLE_APPLICATION_CREDENTIALS"]; Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credentialKey); #endif var configurationProvider = new ConfigurationProvider(); config.Services.Add(typeof(System.Web.Http.ExceptionHandling.IExceptionLogger), ErrorReportingExceptionLogger.Create( configurationProvider.ProjectId, configurationProvider.ServiceName, configurationProvider.ServiceVersion)); //exception filter config.Filters.Add(new CustomExceptionFilterAttribute()); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "v1/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); config.Formatters.JsonFormatter.SupportedMediaTypes .Add(new MediaTypeHeaderValue("text/html")); config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false; UnityConfig.RegisterComponents(); }
private static void Register(HttpConfiguration config) { config.Services.Add(typeof(System.Web.Http.ExceptionHandling.IExceptionLogger), ErrorReportingExceptionLogger.Create(ErrorReportingController.ProjectId, ErrorReportingController.Service, ErrorReportingController.Version)); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "MessageHandler", routeTemplate: "handler/{id}", null, null, new ThrowErrorHandler()); }
// [START register_error_reporting] public static void Register(HttpConfiguration config) { string projectId = "YOUR-PROJECT-ID"; string serviceName = "NAME-OF-YOUR-SERVICE"; string version = "VERSION-OF-YOUR-SERVCICE"; // [START_EXCLUDE] // Check to ensure that projectId has been changed from placeholder value. if (projectId == ("YOUR-PROJECT" + "-ID")) { throw new Exception("Set string projectId above to be your project id."); } // [END_EXCLUDE] // Add a catch all for the uncaught exceptions. config.Services.Add(typeof(IExceptionLogger), ErrorReportingExceptionLogger.Create(projectId, serviceName, version)); // [START_EXCLUDE] var emptyDictionary = new HttpRouteValueDictionary(); // Add our one HttpMessageHandler to the root path. config.Routes.MapHttpRoute("index", "", emptyDictionary, emptyDictionary, new HelloWorldHandler()); // [END_EXCLUDE] }
public static void Register(HttpConfiguration config) { // To enable Google Cloud Stackdrive Logging and Error Reporting // while running on your local machine edit Web.config and uncomment // the <projectId> value under the <log4net> section. Ensure that // the <projectId> is set to a valid Google Cloud Project Id. // [START logging_and_error_reporting] // Check to ensure that projectId has been changed from placeholder value. var section = (XmlElement)ConfigurationManager.GetSection("log4net"); XmlElement element = (XmlElement)section.GetElementsByTagName("projectId").Item(0); string projectId = element?.Attributes["value"].Value; if (projectId == ("YOUR-PROJECT-ID")) { throw new Exception("Update Web.config and replace YOUR-PROJECT-ID" + " with your Google Cloud Project ID, and recompile."); } if (projectId == null) { projectId = Google.Api.Gax.Platform.Instance().GceDetails?.ProjectId; if (projectId == null) { throw new Exception("The logging and error reporting libraries need a project ID. " + "Update Web.config and add a <projectId> entry in the <log4net> section."); } } // [START enable_error_reporting] var serviceName = ConfigurationManager.AppSettings["google_error_reporting:serviceName"]; var version = ConfigurationManager.AppSettings["google_error_reporting:version"]; // Add a catch all to log all uncaught exceptions to Stackdriver Error Reporting. config.Services.Add(typeof(IExceptionLogger), ErrorReportingExceptionLogger.Create(projectId, serviceName, version)); // [END enable_error_reporting] // [START enable_logging] // Retrieve a logger for this context. ILog log = LogManager.GetLogger(typeof(WebApiConfig)); // [END enable_logging] // Log confirmation of set-up to Google Stackdriver Logging. log.Info("Stackdriver Logging with Log4net successfully configured for use."); log.Info("Stackdriver Error Reporting enabled: " + "https://console.cloud.google.com/errors/"); // [END logging_and_error_reporting] // Web API configuration and services // Configure Web API to use only bearer token authentication. config.SuppressDefaultHostAuthentication(); config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
private ErrorReportingExceptionLogger GetLogger(ReportErrorsServiceClient client) { return(ErrorReportingExceptionLogger.Create( Task.FromResult(client), ProjectId, ServiceName, Version)); }