项目作者: daenetCorporation

项目描述 :
Implementation of .netcore ILogger for Azure Event Hub
高级语言: C#
项目地址: git://github.com/daenetCorporation/EventHubLogger.git
创建时间: 2017-07-03T09:47:01Z
项目社区:https://github.com/daenetCorporation/EventHubLogger

开源协议:

下载


EventHubLogger

Implementation of .netcore ILogger for Azure Event Hub

Installation

First, install the Daenet.Common.Logging.EventHub NuGet Package into your application.

  1. Install-Package Daenet.Common.Logging.EventHub

Usage

Following code block, shows how to add EventHubLogger provider to the loggerFactory in EventHubLoggerUnitTests class:

  1. private void initializeEventHubLogger(Func<string, LogLevel, bool> filter,
  2. Func<LogLevel, EventId, object, Exception, EventData> eventDataFormatter = null,
  3. Dictionary<string, object> additionalValues = null)
  4. {
  5. ConfigurationBuilder cfgBuilder = new ConfigurationBuilder();
  6. cfgBuilder.AddJsonFile("EventHubLoggerSettings.json");
  7. var configRoot = cfgBuilder.Build();
  8. ILoggerFactory loggerFactory = new LoggerFactory()
  9. .AddEventHub(configRoot.GetEventHubLoggerSettings(), filter, eventDataFormatter, additionalValues);
  10. m_Logger = loggerFactory.CreateLogger<EventHubLoggerUnitTests>();
  11. }

Following configuration needs to be added in the EventHubLogger>settings.json file.

  1. {
  2. "IncludeScopes": true,
  3. "Switches": {
  4. "Daenet.Common.EventHubLogger.UnitTests": 0
  5. },
  6. "EventHub": {
  7. "ConnectionString": "EventHub Connection String",
  8. "IncludeExceptionStackTrace": false,
  9. "RetryPolicy": 0
  10. }
  11. }

The message logged to EventHub will look like:

  1. "body": {
  2. "Name": "Daenet.Common.EventHubLogger.UnitTests.EventHubLoggerUnitTests",
  3. "Scope": null,
  4. "EventId": "0",
  5. "Message": "Test Warning Log Message",
  6. "Level": 3,
  7. "LocalEnqueuedTime": "2017-07-04T17:32:46.3321255+02:00",
  8. "Exception": null,
  9. "{OriginalFormat}": "Test Warning Log Message"
  10. }