Exemplo n.º 1
0
    async Task <IEnumerable <LogEventEx> > Send(object message)
    {
        var logs      = new List <LogEvent>();
        var eventSink = new EventSink
        {
            Action = logs.Add
        };

        var loggerConfiguration = new LoggerConfiguration();

        loggerConfiguration.Enrich.WithExceptionDetails();
        loggerConfiguration.MinimumLevel.Verbose();
        loggerConfiguration.WriteTo.Sink(eventSink);
        Log.Logger = loggerConfiguration.CreateLogger();
        LogManager.Use <SerilogFactory>();

        var configuration  = ConfigBuilder.BuildDefaultConfig("SerilogTests");
        var serilogTracing = configuration.EnableSerilogTracing();

        serilogTracing.EnableSagaTracing();
        serilogTracing.EnableMessageTracing();

        var recoverability = configuration.Recoverability();

        recoverability.Delayed(settings =>
        {
            settings.TimeIncrease(TimeSpan.FromMilliseconds(1));
            settings.NumberOfRetries(1);
        });
        recoverability.Immediate(settings => { settings.NumberOfRetries(1); });

        configuration.Notifications.Errors.MessageSentToErrorQueue +=
            (sender, retry) => { resetEvent.Set(); };

        var endpoint = await Endpoint.Start(configuration);

        var sendOptions = new SendOptions();

        sendOptions.SetMessageId("00000000-0000-0000-0000-000000000001");
        sendOptions.RouteToThisEndpoint();
        await endpoint.Send(message, sendOptions);

        if (!resetEvent.WaitOne(TimeSpan.FromSeconds(5)))
        {
            throw new Exception("No Set received.");
        }

        await endpoint.Stop();

        Log.CloseAndFlush();

        return(logs.Select(x =>
                           new LogEventEx
        {
            MessageTemplate = x.MessageTemplate,
            Level = x.Level,
            Properties = x.Properties,
            Exception = x.Exception,
        }));
    }
    public async Task Handler()
    {
        resetEvent = new ManualResetEvent(false);
        var configuration = ConfigBuilder.BuildDefaultConfig("WithNoTracingTests");

        configuration.DisableRetries();

        configuration.Notifications.Errors.MessageSentToErrorQueue +=
            (sender, retry) =>
        {
            exception = retry.Exception;
            resetEvent.Set();
        };

        var endpoint = await Endpoint.Start(configuration);

        await endpoint.SendLocal(new StartHandler());

        if (!resetEvent.WaitOne(TimeSpan.FromSeconds(2)))
        {
            throw new Exception("No Set received.");
        }

        await endpoint.Stop();

        Approvals.Verify(exception.Message);
    }
    public async Task Handler()
    {
        Exception?exception     = null;
        var       resetEvent    = new ManualResetEvent(false);
        var       configuration = ConfigBuilder.BuildDefaultConfig("WithNoTracingTests");

        configuration.DisableRetries();
        configuration.RegisterComponents(_ => _.AddSingleton(resetEvent));

        var recoverability = configuration.Recoverability();

        recoverability.Failed(_ => _
                              .OnMessageSentToErrorQueue((message, _) =>
        {
            exception = message.Exception;
            resetEvent.Set();
            return(Task.CompletedTask);
        }));

        var endpoint = await Endpoint.Start(configuration);

        await endpoint.SendLocal(new StartHandler());

        if (!resetEvent.WaitOne(TimeSpan.FromSeconds(2)))
        {
            throw new("No Set received.");
        }

        await endpoint.Stop();

        await Verify(exception !.Message);
    }
Exemplo n.º 4
0
    static async Task <IEnumerable <LogEventEx> > Send(object message, Action <SendOptions>?optionsAction = null)
    {
        logs.Clear();
        var suffix = TypeNameConverter.GetName(message.GetType())
                     .Replace("<", "_")
                     .Replace(">", "_");
        var configuration = ConfigBuilder.BuildDefaultConfig("SerilogTests" + suffix);

        configuration.PurgeOnStartup(true);

        var serilogTracing = configuration.EnableSerilogTracing();

        serilogTracing.EnableSagaTracing();
        serilogTracing.UseHeaderConversion((key, _) =>
        {
            if (key == "ConvertHeader")
            {
                return(new LogEventProperty("NewKey", new ScalarValue("newValue")));
            }

            return(null);
        });
        serilogTracing.EnableMessageTracing();
        ManualResetEvent resetEvent = new(false);

        configuration.RegisterComponents(components => components.RegisterSingleton(resetEvent));

        var recoverability = configuration.Recoverability();

        recoverability.Delayed(settings =>
        {
            settings.TimeIncrease(TimeSpan.FromMilliseconds(1));
            settings.NumberOfRetries(1);
        });
        recoverability.Immediate(settings => { settings.NumberOfRetries(1); });

        recoverability.Failed(settings => settings
                              .OnMessageSentToErrorQueue(_ =>
        {
            resetEvent.Set();
            return(Task.CompletedTask);
        }));

        var endpoint = await Endpoint.Start(configuration);

        SendOptions sendOptions = new();

        optionsAction?.Invoke(sendOptions);
        sendOptions.SetMessageId("00000000-0000-0000-0000-000000000001");
        sendOptions.RouteToThisEndpoint();
        await endpoint.Send(message, sendOptions);

        if (!resetEvent.WaitOne(TimeSpan.FromSeconds(10)))
        {
            throw new("No Set received.");
        }

        await endpoint.Stop();

        return(logs.Select(x =>
                           new LogEventEx
                           (
                               messageTemplate: x.MessageTemplate,
                               level: x.Level,
                               properties: x.Properties,
                               exception: x.Exception
                           )));
    }