Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mention in the README.md that InMemorySink.Instance is AsyncLocal #26

Open
hiredgunhouse opened this issue Jan 18, 2022 · 4 comments
Open

Comments

@hiredgunhouse
Copy link

hiredgunhouse commented Jan 18, 2022

It is not mentioned in the README.md that InMemorySink.Instance is actually AsyncLocal which is quite important piece of information. I thought that InMemorySink.Instance is buggy when it was not behaving as I was expected: Instance is typically a singleton. I only discovered that important fact through issues.

Also you could mention that it is possibly important if you mix InMemorySink with integration tests based on WebApplicationFactory, that was my use-case where the AsyncLocal completely surprised me.

BTW. So far I love it, thank you for your contribution, InMemorySink combined with the assertion library turns tests into little things of beauty :)

@sandermvanvliet
Copy link
Contributor

Thanks for the feedback @hiredgunhouse!

I’ve not been focusing a lot on the package recently but should have some time soon to add some updates including some better docs around the thread safety and async local stuff.

I had been experimenting with something a bit more robust but that proves very difficult depending on test framework as well as the logger itself but I agree better docs would be helpful 👍

@Skulblaka
Copy link

I'm also using InMemorySink with WebApplicationFactory and ran into probably the same problem as @hiredgunhouse. What does your solution look like?

I ended up creating a separate instance of InMemorySink and registering it in the DI container (since we're using ReadFrom.Configuration anyway) using _factory.WithWebHostBuilder(builder => builder.ConfigureServices(services => services.AddSingleton<ILogEventSink>(sink))) for each test...

@sandermvanvliet
Copy link
Contributor

That is the better approach and guarantees you’ve got isolated instances.

@michaeltg17
Copy link

michaeltg17 commented May 22, 2024

I tried to use the static InMemorySink.Instance but got nothing logged. I ended up creating a new InMemorySink instance for each test (in the test class constructor) and assigning it to a property in WebApplicationFactoryFixture. There it is being used when creating the host using the Map sink because otherwise I only get the first test logged.

configuration.WriteTo.Map(
    _ => InMemorySink,
    (_, writeTo) => writeTo.Sink(InMemorySink),
    sinkMapCountLimit: 1);

Maybe there is a better way...

In any case, great sink!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants