- 2 minutes to read

2.0.0 Event Hub Example

Logging with a URI

Program.cs

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Core;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices((context, services) =>
    {
        var configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();

        var logger = new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .CreateLogger();
   
        services.AddSingleton<ILogger, Logger>(sp => logger);

    })
    .Build();

host.Run();

appsettings.json

{
  "Serilog": {
    "Using": [ "<placeholder>" ],
    "WriteTo": [
      {
        "Name": "NodiniteApiSink",
        "Args": {
          "NodiniteApiUrl": "https://fancysrv01/nodinite/dev/logapi/",
          "Settings": {
            "LogAgentValueId": 503,
            "EndPointName": "Nodinite.Serilog.Sink.Tests",
            "EndPointUri": "Nodinite.Serilog.Sink.Tests.Serilog",
            "EndPointDirection": 0,
            "EndPointTypeId": 0,
            "OriginalMessageTypeName": "Orders.LogEvent/1.0",
            "ProcessingUser": "NODINITE",
            "ProcessName": "Nodinite.Serilog.Sink.Tests",
            "ProcessingMachineName": "NODINITE-DEV",
            "ProcessingModuleName": "DOTNETCORE.TESTS",
            "ProcessingModuleType": "DOTNETCORE.TESTPROJECT"
          }
        }
      }
    ]
  }
}

Function.cs


using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Serilog.Core.Enrichers;

namespace isolatedFun_net6_string
{
    public class Function1
    {
        private readonly Serilog.ILogger _logger;
        public Function1(Serilog.ILogger logger)
        {
            _logger = logger;
        }

        [Function("Function1")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
        {
            _logger.Information("5");

            ((Serilog.Core.Logger)_logger).Dispose();
            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
            response.WriteString("Welcome to Azure Functions!");

            return response;
        }
    }
}

Dependencies in .csproj


  <ItemGroup>
    <PackageReference Include="Azure.Identity" Version="1.8.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.8.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0" />

    <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />

    <PackageReference Include="Nodinite.Serilog.ApiSink" Version="<----------------->" />

  </ItemGroup>