Microsoft extension logging with ASP.NET MVC & VB.NET and Unity dependency injection

249 Views Asked by At

I'm trying to use Microsoft extension logging in ASP.NET MVC 5 with dependency injection using Unity but its not showing any logs in the console. Here are my complete code,

Global.asax.vb:

Sub Application_Start()
  'Registering Unity container
  Dim container = New UnityContainer()
  container.RegisterType(Of ILoggingService, LoggingService)()
  DependencyResolver.SetResolver(New UnityDependencyResolver(container))
  
  'Initiazlize Global logging
   Dim loggerOptions = New LoggerFilterOptions With {.MinLevel = LogLevel.Information}
   Dim loggerFactory = New LoggerFactory(Array.Empty(Of ILoggerProvider), loggerOptions)
   Dim logger = loggerFactory.CreateLogger(Of MvcApplication)
   logger?.LogWarning("Logging Initialized at Global level.") ' I dont see this line printing anywhere
End Sub

ILoggingService interface:

Public Interface ILoggingService
    Sub CreateLog(strLogText As String)
End Interface

LoggingService implementation:

Imports Microsoft.Extensions.Logging

Public Class LoggingService
    Implements ILoggingService

    Private Sub CreateLog(strLogText As String) Implements ILoggingService.CreateLog
        Dim loggerOptions = New LoggerFilterOptions With {.MinLevel = LogLevel.Information}
        Dim loggerFactory = New LoggerFactory(Array.Empty(Of ILoggerProvider), loggerOptions)
        Dim logger = loggerFactory.CreateLogger(Of MvcApplication)
        logger?.LogWarning(strLogText)
    End Sub
  End Class

Account controller:

Public Class AccountController
    Inherits BaseController
Public ReadOnly m_logger As ILoggingService
    Sub New(ByVal logger As ILoggingService)
        m_logger = logger
    End Sub
 Public Function LogOn(returnUrl As String) As ActionResult
        m_logger.CreateLog("Logon controller has been called.") 'Doesnt print anything
 End Function
End Class

I am unsure where am I wrong whether using the logging provider, or injection.

0

There are 0 best solutions below