public void ProcessRunnerFailedWithErrorMessage([Values(true, false)] bool logPII) { string log = string.Empty; using AzureEventSourceListener listener = new AzureEventSourceListener((args, s) => { log = $"{args.EventName} {s}"; }, EventLevel.Verbose); var error = "Test error"; var process = new TestProcess { Error = error }; using var runner = new ProcessRunner(process, TimeSpan.FromSeconds(30), logPII, default); var exception = Assert.CatchAsync <InvalidOperationException>(async() => await Run(runner)); Assert.AreEqual(error, exception.Message); if (logPII) { Assert.That(log, Contains.Substring(error)); Assert.That(log, Contains.Substring(nameof(AzureIdentityEventSource.ProcessRunnerError))); } else { Assert.That(log, Does.Not.Contain(error)); Assert.That(log, Does.Not.Contain(nameof(AzureIdentityEventSource.ProcessRunnerError))); } }
public void LoggingLevel() { #region Snippet:LoggingLevel using AzureEventSourceListener consoleListener = AzureEventSourceListener.CreateConsoleLogger(EventLevel.Warning); using AzureEventSourceListener traceListener = AzureEventSourceListener.CreateTraceLogger(EventLevel.Informational); #endregion }
public async Task ProcessRunnerSucceeded([Values(true, false)] bool logPII) { var output = "Test output"; var fileName = "someFileName.exe"; var args = "arg1 arg2"; var pi = new ProcessStartInfo(fileName, args); var process = new TestProcess { StartInfo = pi, Output = output }; string result; string log = string.Empty; using AzureEventSourceListener listener = new AzureEventSourceListener((args, s) => { log = $"{args.EventName} {s}"; }, EventLevel.Verbose); using var runner = new ProcessRunner(process, TimeSpan.FromSeconds(30), logPII, default); result = await Run(runner); Assert.AreEqual(output, result); if (logPII) { Assert.That(log, Contains.Substring(fileName)); Assert.That(log, Contains.Substring(args)); Assert.That(log, Contains.Substring(nameof(AzureIdentityEventSource.ProcessRunnerInformational))); } else { Assert.That(log, Does.Not.Contain(fileName)); Assert.That(log, Does.Not.Contain(args)); Assert.That(log, Does.Not.Contain(nameof(AzureIdentityEventSource.ProcessRunnerInformational))); } }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { var rc = new TokenRequestContext(new string[] { "https://graph.microsoft.com/.default" }); AzureEventSourceListener.CreateConsoleLogger(); services.AddControllersWithViews(); services.AddRazorPages(); // Configure the credential options var options = new DefaultAzureCredentialOptions { ExcludeVisualStudioCodeCredential = true, ExcludeVisualStudioCredential = true, Diagnostics = { IsLoggingEnabled = true } }; // Create a DefaultAzureCredential var cred = new DefaultAzureCredential(options); // Initialize a BlobContainerClient with the credential. services.AddSingleton(new BlobContainerClient(new Uri(Configuration["BlobUri"]), cred)); // Initialize a Graph client with the credential. GraphServiceClient graphServiceClient = new GraphServiceClient( new DelegateAuthenticationProvider((requestMessage) => { requestMessage .Headers .Authorization = new AuthenticationHeaderValue("Bearer", cred.GetToken(rc).Token); return(Task.CompletedTask); })); services.AddSingleton(graphServiceClient); }
public async Task VerifyCancelCertificateOperation() { // Log details why this fails often for live tests on net461. using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger(EventLevel.Verbose); string certName = Recording.GenerateId(); CertificatePolicy certificatePolicy = DefaultPolicy; CertificateOperation operation = await Client.StartCreateCertificateAsync(certName, certificatePolicy); RegisterForCleanup(certName); try { await operation.CancelAsync(); } catch (RequestFailedException e) when(e.Status == 403) { Assert.Inconclusive("The create operation completed before it could be canceled."); } OperationCanceledException ex = Assert.ThrowsAsync <OperationCanceledException>(() => WaitForCompletion(operation)); Assert.AreEqual("The operation was canceled so no value is available.", ex.Message); Assert.IsTrue(operation.HasCompleted); Assert.IsFalse(operation.HasValue); Assert.AreEqual(200, operation.GetRawResponse().Status); }
/// <summary> /// Waits for replication to become in status "Mirrored" /// </summary> /// <param name="client">ANF Client</param> /// <param name="resourceId">Resource Id of the resource being waited for being deleted</param> /// <param name="intervalInSec">Time in seconds that the sample will poll to check if the resource got deleted or not. Defaults to 10 seconds.</param> /// <param name="retries">How many retries before exting the wait for no resource function. Defaults to 60 retries.</param> /// <returns></returns> static public async Task WaitForCompleteReplicationStatus(AzureNetAppFilesManagementClient client, string resourceId, int intervalInSec = 10, int retries = 60) { using AzureEventSourceListener listener = AzureEventSourceListener.CreateTraceLogger(EventLevel.Verbose); for (int i = 0; i < retries; i++) { System.Threading.Thread.Sleep(TimeSpan.FromSeconds(intervalInSec)); try { var status = await client.Volumes.ReplicationStatusMethodAsync(ResourceUriUtils.GetResourceGroup(resourceId), ResourceUriUtils.GetAnfAccount(resourceId), ResourceUriUtils.GetAnfCapacityPool(resourceId), ResourceUriUtils.GetAnfVolume(resourceId)); if (status.MirrorState.ToLower().Equals("mirrored")) { break; } } catch (Exception ex) { if (!(ex.Message.ToLower().Contains("creating") && ex.Message.ToLower().Contains("replication"))) { throw; } } } }
public void TraceLogging() { #region Snippet:TraceLogging // Setup a listener to monitor logged events. using AzureEventSourceListener listener = AzureEventSourceListener.CreateTraceLogger(); #endregion }
public void Logging() { #region Snippet:ConsoleLogging // Setup a listener to monitor logged events. using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger(EventLevel.LogAlways); #endregion }
public async Task TraceListener() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:EventHubs_Sample10_TraceListener #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; #endif var producer = new EventHubProducerClient(connectionString, eventHubName); using AzureEventSourceListener traceListener = AzureEventSourceListener.CreateTraceLogger(EventLevel.LogAlways); try { var events = new[] { new EventData("EventOne"), new EventData("EventTwo") }; await producer.SendAsync(events); } finally { await producer.CloseAsync(); } #endregion }
public async Task CredentialTypeLogged(Type availableCredential) { List <string> messages = new(); using AzureEventSourceListener listener = new AzureEventSourceListener( (_, message) => messages.Add(message), EventLevel.Informational); var expToken = new AccessToken(Guid.NewGuid().ToString(), DateTimeOffset.MaxValue); List <Type> calledCredentials = new(); var credFactory = GetMockDefaultAzureCredentialFactory(availableCredential, expToken, calledCredentials); var options = new DefaultAzureCredentialOptions { ExcludeEnvironmentCredential = false, ExcludeManagedIdentityCredential = false, ExcludeSharedTokenCacheCredential = false, ExcludeAzureCliCredential = false, ExcludeInteractiveBrowserCredential = false, }; var cred = new DefaultAzureCredential(credFactory, options); await cred.GetTokenAsync(new TokenRequestContext(MockScopes.Default)); Assert.That(messages, Has.Some.Match(availableCredential.Name).And.Some.Match("DefaultAzureCredential credential selected")); }
public static async Task Main(string[] args) { // Forward all the events written to the console output with a specific format. using AzureEventSourceListener listener = new AzureEventSourceListener( (e, message) => Console.WriteLine("[{0:HH:mm:ss:fff}][{1}] {2}", DateTimeOffset.Now, e.Level, message), level: EventLevel.Verbose); // Client init samples ModelResolutionSamples.ClientInitialization(); // Model Resolution samples await ModelResolutionSamples.GetModelsFromGlobalRepoAsync(); await ModelResolutionSamples.GetModelsFromLocalRepoAsync(); await ModelResolutionSamples.TryGetModelsFromGlobalRepoButNotFoundAsync(); await ModelResolutionSamples.TryGetModelsFromLocalRepoButNotFoundAsync(); await ModelResolutionSamples.TryGetModelsWithInvalidDtmiAsync(); // Parser integration samples await ParserIntegrationSamples.GetModelsAndParseAsync(); await ParserIntegrationSamples.ParseAndGetModelsWithExtensionAsync(); }
public async Task TraceListener() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:EventHubs_Sample10_TraceListener #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; #endif var producer = new EventHubProducerClient(connectionString, eventHubName); using AzureEventSourceListener traceListener = AzureEventSourceListener.CreateTraceLogger(EventLevel.LogAlways); try { using var eventBatch = await producer.CreateBatchAsync(); var eventData = new EventData("This is an event body"); if (!eventBatch.TryAdd(eventData)) { throw new Exception($"The event could not be added."); } } finally { await producer.CloseAsync(); } #endregion }
public void LoggingCallback() { #region Snippet:LoggingCallback using AzureEventSourceListener listener = new AzureEventSourceListener( (e, message) => Console.WriteLine("[{0:HH:mm:ss:fff}][{1}] {2}", DateTimeOffset.Now, e.Level, message), level: EventLevel.Verbose); #endregion }
public void LoggingCallback() { #region Snippet:LoggingCallback using AzureEventSourceListener listener = new AzureEventSourceListener( (e, message) => Console.WriteLine($"{DateTime.Now} {message}"), level: EventLevel.Verbose); #endregion }
/// <inheritdoc/> public void Dispose() { if (_listener != null) { _listener.Dispose(); _listener = null; } }
/// <summary> /// Creates a new instance of the <see cref="SynapseTestEventListener"/> class. /// </summary> public SynapseTestEventListener() { // Log responses when running live tests for diagnostics. if ("Live".Equals(Environment.GetEnvironmentVariable("AZURE_SYNAPSE_TEST_MODE"), StringComparison.OrdinalIgnoreCase)) { _listener = AzureEventSourceListener.CreateConsoleLogger(EventLevel.Verbose); } }
static async Task Main(string[] args) { using AzureEventSourceListener listener = AzureEventSourceListener.CreateTraceLogger(EventLevel.Verbose); await ResolveAndParse(); await ParseAndResolve(); await NotFound(); }
public async Task CustomListenerWithFile() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:EventHubs_Sample10_CustomListenerWithFile #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; var producer = new EventHubProducerClient(connectionString, eventHubName); using Stream stream = new FileStream("<< PATH TO THE FILE >>", FileMode.OpenOrCreate, FileAccess.Write); #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; var producer = new EventHubProducerClient(connectionString, eventHubName); using Stream stream = new MemoryStream(); #endif using StreamWriter streamWriter = new StreamWriter(stream) { AutoFlush = true }; using AzureEventSourceListener customListener = new AzureEventSourceListener((args, message) => { if (args.EventSource.Name.StartsWith("Azure-Messaging-EventHubs")) { switch (args.Level) { case EventLevel.Error: streamWriter.Write(message); break; default: Console.WriteLine(message); break; } } }, EventLevel.LogAlways); try { using var eventBatch = await producer.CreateBatchAsync(); var eventData = new EventData("This is an event body"); if (!eventBatch.TryAdd(eventData)) { throw new Exception($"The event could not be added."); } } finally { await producer.CloseAsync(); } #endregion }
protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing && _azureEventSourceListener != null) { _azureEventSourceListener.Dispose(); _azureEventSourceListener = null; } }
public async Task Run() { // Create a new metrics instance for the given test run. _metrics = new Metrics(_testConfiguration.InstrumentationKey); // Set up cancellation token using var enqueueingCancellationSource = new CancellationTokenSource(); var runDuration = TimeSpan.FromHours(_testConfiguration.DurationInHours); enqueueingCancellationSource.CancelAfter(runDuration); using var azureEventListener = new AzureEventSourceListener(SendHeardException, EventLevel.Error); var enqueuingTasks = default(IEnumerable <Task>); // Start two buffered producer background tasks try { enqueuingTasks = Enumerable .Range(0, 2) .Select(_ => Task.Run(() => new BufferedPublisher(_testConfiguration, _metrics).Start(enqueueingCancellationSource.Token))) .ToList(); // Periodically update garbage collection metrics while (!enqueueingCancellationSource.Token.IsCancellationRequested) { UpdateEnvironmentStatistics(_metrics); await Task.Delay(TimeSpan.FromMinutes(1), enqueueingCancellationSource.Token).ConfigureAwait(false); } await Task.WhenAll(enqueuingTasks).ConfigureAwait(false); } catch (TaskCanceledException) { // No action needed. } catch (Exception ex) when (ex is OutOfMemoryException || ex is StackOverflowException || ex is ThreadAbortException) { _metrics.Client.TrackException(ex); Environment.FailFast(ex.Message); } catch (Exception ex) { _metrics.Client.TrackException(ex); } finally { // Flush and wait for all metrics to be aggregated and sent to application insights _metrics.Client.Flush(); await Task.Delay(60000).ConfigureAwait(false); } }
public void CanCreateMultipleEventSources() { EventSource GetEventSource(TestAssemblyLoadContext testAssemblyLoadContext) { return((EventSource)Activator.CreateInstance(testAssemblyLoadContext.Assemblies .Single() .GetType("Azure.Core.Tests.AzureEventSourceTests+TestEventSource"))); } void LogEvent(EventSource azureCoreEventSource) { azureCoreEventSource.GetType() .GetMethod("LogSomething", BindingFlags.Public | BindingFlags.Instance) .Invoke(azureCoreEventSource, Array.Empty <object>()); } var alc = new TestAssemblyLoadContext("Test 1"); var alc2 = new TestAssemblyLoadContext("Test 2"); try { List <EventWrittenEventArgs> events = new(); using var listener = new AzureEventSourceListener((args, s) => events.Add(args), EventLevel.Verbose); alc.LoadFromAssemblyPath(typeof(TestEventSource).Assembly.Location); alc2.LoadFromAssemblyPath(typeof(TestEventSource).Assembly.Location); using var es0 = new TestEventSource(); using var es1 = GetEventSource(alc); using var es2 = GetEventSource(alc2); LogEvent(es0); LogEvent(es1); LogEvent(es2); Assert.AreEqual("Azure-Corez", es0.Name); Assert.AreEqual("Azure-Corez-1", es1.Name); Assert.AreEqual("Azure-Corez-2", es2.Name); Assert.AreEqual(3, events.Count); Assert.AreEqual("Azure-Corez", events[0].EventSource.Name); Assert.AreEqual("Azure-Corez-1", events[1].EventSource.Name); Assert.AreEqual("Azure-Corez-2", events[2].EventSource.Name); } finally { alc.Unload(); alc2.Unload(); } }
public async Task CustomListenerWithFilter() { await using var scope = await EventHubScope.CreateAsync(1); #region Snippet:EventHubs_Sample10_CustomListenerWithFilter #if SNIPPET var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>"; var eventHubName = "<< NAME OF THE EVENT HUB >>"; #else var connectionString = EventHubsTestEnvironment.Instance.EventHubsConnectionString; var eventHubName = scope.EventHubName; #endif var producer = new EventHubProducerClient(connectionString, eventHubName); using AzureEventSourceListener customListener = new AzureEventSourceListener((args, message) => { if (args.EventSource.Name.StartsWith("Azure-Identity") && args.Level == EventLevel.Verbose) { Trace.WriteLine(message); } else if (args.EventSource.Name.StartsWith("Azure-Messaging-EventHubs")) { switch (args.EventId) { case 3: // Publish Start case 4: // Publish Complete case 5: // Publish Error Console.WriteLine(message); break; } } }, EventLevel.LogAlways); try { using var eventBatch = await producer.CreateBatchAsync(); var eventData = new EventData("This is an event body"); if (!eventBatch.TryAdd(eventData)) { throw new Exception($"The event could not be added."); } } finally { await producer.CloseAsync(); } #endregion }
private static CommandLineBuilder GetCommandLine() { RootCommand root = new RootCommand("parent") { Description = $"Microsoft IoT Models Repository CommandLine v{Outputs.CommandLineVersion}" }; root.Name = Outputs.RootCommandName; root.Add(BuildExportCommand()); root.Add(BuildValidateCommand()); root.Add(BuildImportModelCommand()); root.Add(BuildRepoIndexCommand()); root.Add(BuildRepoExpandCommand()); root.AddGlobalOption(CommonOptions.Debug); root.AddGlobalOption(CommonOptions.Silent); CommandLineBuilder builder = new CommandLineBuilder(root); builder.UseMiddleware(async(context, next) => { AzureEventSourceListener listener = null; try { if (context.ParseResult.Tokens.Any(x => x.Type == TokenType.Option && x.Value == "--debug")) { Outputs.WriteHeader(); listener = AzureEventSourceListener.CreateConsoleLogger(EventLevel.Verbose); Outputs.WriteDebug(context.ParseResult.ToString()); } if (context.ParseResult.Tokens.Any(x => x.Type == TokenType.Option && x.Value == "--silent")) { System.Console.SetOut(TextWriter.Null); } await next(context); } finally { if (listener != null) { listener.Dispose(); } } }); return(builder); }
public void SetTransportOptions([Values(true, false)] bool isCustomTransportSet) { using var testListener = new TestEventListener(); testListener.EnableEvents(AzureCoreEventSource.Singleton, EventLevel.Verbose); var transport = new MockTransport(new MockResponse(503), new MockResponse(200)); var options = new TestOptions(); if (isCustomTransportSet) { options.Transport = transport; } List <EventWrittenEventArgs> events = new(); using var listener = new AzureEventSourceListener( (args, s) => { events.Add(args); }, EventLevel.Verbose); var pipeline = HttpPipelineBuilder.Build( options, Array.Empty <HttpPipelinePolicy>(), Array.Empty <HttpPipelinePolicy>(), new HttpPipelineTransportOptions(), ResponseClassifier.Shared); HttpPipelineTransport transportField = pipeline.GetType().GetField("_transport", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField).GetValue(pipeline) as HttpPipelineTransport; if (isCustomTransportSet) { Assert.That(transportField, Is.TypeOf <MockTransport>()); events.Any( e => e.EventId == 23 && e.EventName == "PipelineTransportOptionsNotApplied" && e.GetProperty <string>("optionsType") == options.GetType().FullName); } else { Assert.That(transportField, Is.Not.TypeOf <MockTransport>()); } }
public void Logging() { // Retrieve the connection string from the configuration store. // You can get the string from your Azure portal. var connectionString = Environment.GetEnvironmentVariable("APPCONFIGURATION_CONNECTION_STRING"); var client = new ConfigurationClient(connectionString); // Setup a listener to monitor logged events. using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger(EventLevel.LogAlways); Response <ConfigurationSetting> setResponse = client.Set(new ConfigurationSetting("some_key", "some_value")); if (setResponse.GetRawResponse().Status != 200) { throw new Exception("could not set configuration setting"); } // Delete the setting when you don't need it anymore. client.Delete("some_key"); }
public static void LoggingWithFilters() { #region Snippet:LoggingWithFilters using AzureEventSourceListener listener = new AzureEventSourceListener((args, message) => { if (args.EventSource.Name.StartsWith("Azure-Identity") && args.Level == EventLevel.Verbose) { Trace.WriteLine(message); } else if (args.EventSource.Name.StartsWith("Azure-Messaging-EventHubs")) { switch (args.EventId) { case 3: // Event Publish Start case 4: // Event Publish Complete case 5: // Event Publish Error Console.WriteLine(message); break; } } }, EventLevel.LogAlways); #endregion }
public static void FileLogging() { #region Snippet:FileLogging #if SNIPPET using Stream stream = new FileStream( "<< PATH TO FILE >>", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); #else using Stream stream = new MemoryStream(); #endif using StreamWriter streamWriter = new StreamWriter(stream) { AutoFlush = true }; using AzureEventSourceListener listener = new AzureEventSourceListener((args, message) => { if (args.EventSource.Name.StartsWith("Azure-Identity")) { switch (args.Level) { case EventLevel.Error: streamWriter.Write(message); break; default: Console.WriteLine(message); break; } } }, EventLevel.LogAlways); #endregion }
public async Task VerifyCancelCertificateOperation() { // Log details why this fails often for live tests on net461. using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger(EventLevel.Verbose); string certName = Recording.GenerateId(); CertificatePolicy certificatePolicy = DefaultPolicy; CertificateOperation operation = await Client.StartCreateCertificateAsync(certName, certificatePolicy); RegisterForCleanup(certName); try { await Client.CancelCertificateOperationAsync(certName); } catch (RequestFailedException ex) when(ex.Status == 403) { Assert.Inconclusive("The create operation completed before it could be canceled."); } Assert.ThrowsAsync <OperationCanceledException>(() => WaitForCompletion(operation)); }
public static void Main(string[] args) { using var listener = AzureEventSourceListener.CreateConsoleLogger(); CreateHostBuilder(args).Build().Run(); }
public KeyVaultProxyTests(SecretsFixture fixture) { _fixture = fixture ?? throw new ArgumentNullException(nameof(fixture)); _logger = AzureEventSourceListener.CreateConsoleLogger(EventLevel.Verbose); }