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

Cannot post event in .Net Core when SEQ base url has relative path #108

Open
calvin998 opened this issue Jul 2, 2018 · 5 comments
Open

Comments

@calvin998
Copy link

It seems this issue exists in .Net Core environment only. When the SEQ base address doesn't have a relative path (e.g. http://myseqserver.com) it works fine. Once the configuration is changed to http://myseqserver.com/seq, the local debugging message will display

Received failed result NotFound when posting events to Seq

Further investigation showed that when HttpClient.BaseAddress is set this way, the relative path will be ignored in PostAsync(). I checked the Request data url in the returned data of PostAsync() and found that PostAsync() always stripes out the relative path and merges the Host address with PostAsync() target. So:

_httpClient.BaseAddress = new Uri(SeqApi.NormalizeServerBaseAddress("http://myseqserver.com/seq"));
var result = await _httpClient.PostAsync("api/events/raw", content).ConfigureAwait(false);

ends up only posting to "http://myseqserver.com/api/events/raw".

To get around this strange behavior, you have to parse the base address and set it the relative path explicitly:

_httpClient.BaseAddress = new Uri(new Uri("http://myseqserver.com/"), "seq/");

Also the relative path must end with slash or it will be ignored.

In my environment we have api gateway before the SEQ instance. Many services share the same DNS entry so we have to use relative path. Adding support for relative path in SEQ address will really helps.

Thanks!

nblumhardt added a commit to nblumhardt/serilog-sinks-seq that referenced this issue Jul 2, 2018
@nblumhardt
Copy link
Member

Thanks for the report!

I'm having some trouble reproducing this with Serilog.Sinks.Seq running on .NET Core 2.0 on Windows, against a Seq 4.2.1232 server.

Could you please let me know the versions, client, and server operating systems you're working with?

Cheers,
Nick

@calvin998
Copy link
Author

calvin998 commented Jul 2, 2018

Hi Nick,

This is the reference of my project

<PackageReference Include="Serilog" Version="2.7.1" />
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />

I tried both Net Core 2.0 web api and console with same result. The server is 3.4.20 but I don't think it matters because the issue is at the client side. This is how I config the SeriLog

var logConfig = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithProperty("AppType", logSetting.AppType)
.Enrich.WithProperty("AppName", logSetting.AppName)
.Enrich.WithProperty("Team", logSetting.Team);
logConfig = logConfig.Enrich.With<LoggingHttpEnricher>();
logConfig.WriteTo.Seq(logSetting.seqServerUrl, compact: true);

I also have this to dump client error code on my console:

Serilog.Debugging.SelfLog.Enable(Console.Error);

When the above logSetting.seqServerUrl is http://somednc.com/seq, the error I got back is from this line in Seq sink - http status is 404. Once I changed my setting to IP:port (without relative path), it works fine. Exact same code and same SEQ server.

I used this code snippet to verify the HttpClient behavior:

var _httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri("https://myserver.com/seq/");
var content = new StringContent("{ 'Events': [{ 'Timestamp': '2018-07-02T22:09:08.12345+10:00','Level': 'Warning', 'MessageTemplate': 'Testing from {Drive}', 'Properties': { 'Drive': 'oceans 12' }}]}", Encoding.UTF8, "application/json");
var result = _httpClient.PostAsync("api/events/raw", content).Result;

If you check the Request property of the result variable, you will see the actual request uri is "https://myserver.com/api/events/raw" - /seq/ is removed. But this line works:

_httpClient.BaseAddress = new Uri(new Uri("https://myserver.com/"), "seq/");

My .NET 4.x code works fine with relative path in the SEQ url. I guess that's because the HttpClient implementation of Net 4.x is different.

@nblumhardt
Copy link
Member

Thanks for the follow-up. I'm still struggling to wrap my head around this or reproduce it (though I don't doubt something's going on there!)

Once I changed my setting to IP:port (without relative path), it works fine.

Do you have two Seq instances listening on the host? (If you omit the relative path, this will hit the root Seq instance.)

Could you please let me know what you see (it should be JSON) if you access the URL https://myserver.com/seq/api in a browser? Thanks!

@calvin998
Copy link
Author

calvin998 commented Jul 3, 2018 via email

@nblumhardt
Copy link
Member

Thanks for the detailed follow-up! 👍

I'm trying to figure out why this would occur in your case, but not elsewhere given the same library and URI setup... (It's important when making changes here for us to understand why they're necessary, both so that we can give adequate support, and so that we don't inadvertently break fixes with later changes). The best I can come up with is that there's a bugfix or breakage in a point version of the Uri class that's affecting us - it seems like UriKind.RelativeOrAbsolute and friends are a bit ........ crufty....

I think our best bet here is to fully-form the URI when the sink is configured, and use SendAsync() directly as you've suggested.

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

No branches or pull requests

2 participants