GithubHelp home page GithubHelp logo

Comments (10)

FantasticFiasco avatar FantasticFiasco commented on June 14, 2024 1

I wouldn't copy the custom HTTP client from my test assembly, it contain a lot of stuff that are only relevant for tests. You should go with something that looks like this:

public class SerilogHttpClient : IHttpClient
{
    private readonly HttpClient httpClient;

    public SerilogHttpClient()
    {
        var handler = new HttpClientHandler { UseDefaultCredentials = true };
        httpClient = new HttpClient(handler);
    }
        
    public Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content) => httpClient.PostAsync(requestUri, content);

    public void Dispose() => httpClient?.Dispose();
}

from serilog-sinks-http.

FantasticFiasco avatar FantasticFiasco commented on June 14, 2024

Hi daver77!

Are you experiencing the exception in tests or in your running application? Can you point me to your code or perhaps provide a sample application that experience the problem?

from serilog-sinks-http.

daver77 avatar daver77 commented on June 14, 2024

Hi, I get the error when the Log is initialising

Log.Logger = new LoggerConfiguration()
                .ReadFrom.AppSettings()
                .CreateLogger();
  <appSettings>
    <add key="serilog:minimum-level" value="Verbose" />
    <add key="serilog:using:Http" value="Serilog.Sinks.Http" />
    <add key="serilog:write-to:Http"/>
    <add key="serilog:write-to:Http.requestUri" value="http://myurl/api/serilog/5" />
    <add key="serilog:write-to:Http.httpClient" value="SerilogTest.SerilogHttpClient, SerilogTest" />
  </appSettings>
namespace SerilogTest
{
    /// <summary>
    /// An implementation of IHttpClient to log errors via the Serilog HTTP sink
    /// </summary>
    public class SerilogHttpClient : IHttpClient
    {
        private int numberOfPosts;

        public SerilogHttpClient()
        {
            Instance = this;
        }
        
        public static SerilogHttpClient Instance { get; set; }

        public HttpClient Client { get; set; }

        public int NumberOfPosts => numberOfPosts;

        public async Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)
        {
            using (var handler = new HttpClientHandler { UseDefaultCredentials = true })
            using (Client = new HttpClient(handler))
            {
                Interlocked.Increment(ref numberOfPosts);

                return await Client.PostAsync(requestUri, content);
            }
        }

        public void Dispose()
        {
            Client?.Dispose();
            Client = null;
        }
    }
}

from serilog-sinks-http.

FantasticFiasco avatar FantasticFiasco commented on June 14, 2024

I'll build a sample application looking like yours to see if I experience the same problem.

from serilog-sinks-http.

daver77 avatar daver77 commented on June 14, 2024

SerilogTest.zip

Sample project attached if it helps

from serilog-sinks-http.

FantasticFiasco avatar FantasticFiasco commented on June 14, 2024

Nice, I'll take a look at it!

from serilog-sinks-http.

FantasticFiasco avatar FantasticFiasco commented on June 14, 2024

I've just finished putting together a very basic example showing this sink writing log events to a log server over HTTP, you can find it here: https://github.com/FantasticFiasco/serilog-sinks-http-sample-dotnet-framework

While I take a look at your code, can you try to run my sample and see if you experience any problems?

from serilog-sinks-http.

FantasticFiasco avatar FantasticFiasco commented on June 14, 2024

I have verified that I get the same exception using your sample, but don't yet know why.

from serilog-sinks-http.

FantasticFiasco avatar FantasticFiasco commented on June 14, 2024

Something is wrong with your project. I cannot run your attached sample unless I remove all NuGet packages and then add them again. It is not specific to .NET 4.6.2 because it doesn't work in .NET 4.6.1 either.

I tried creating a new project from scratch:

  1. Start Visual Studio
  2. Create new project of type ASP.NET Web Application (.NET Framework) and make sure .NET Framework 4.6.2 is selected
  3. Chose Web Forms as template
  4. When solution is loaded, add NuGet package Serilog.Sinks.Http
  5. Add the following class to the project:
public class SerilogHttpClient : IHttpClient
{
    private readonly HttpClient httpClient;

    public SerilogHttpClient()
    {
        httpClient = new HttpClient();
    }
        
    public Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)
    {
        return httpClient.PostAsync(requestUri, content);
    }

    public void Dispose() => httpClient?.Dispose();
}
  1. In Global.asax in Application_Start method append the following lines of code:
Log.Logger = new LoggerConfiguration()
    .WriteTo.Http("http://myurl/api/serilog/5", httpClient: new SerilogHttpClient())
    .CreateLogger();

    Log.Error("Error");
  1. Start the application and verify that no exception is thrown

from serilog-sinks-http.

FantasticFiasco avatar FantasticFiasco commented on June 14, 2024

I am closing this issue due to inactivity. I assume your problem was solved since no other issues where raised.

from serilog-sinks-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.