Imports Mercury.Mobile.Threading
Imports System.Runtime.CompilerServices

Namespace Logging

    ''' <summary>
    ''' Extensions to the core logging classes.
    ''' </summary>
    ''' <remarks></remarks>
    Public Module LogExtensions

        Private ReadOnly _loggers As ThreadSafeDictionary(Of String, ILog) = New ThreadSafeDictionary(Of String, ILog)()

        ''' <summary>
        ''' Returns the logger with the specified log name.
        ''' </summary>
        ''' <typeparam name="T">The type of the instance being logged.</typeparam>
        ''' <param name="type">The instance being logged.</param>
        ''' <param name="logName">The name of the logger to use.</param>
        ''' <returns>The logger with the specified log name.</returns>
        <Extension()> _
        Public Function Log(Of T)(ByVal type As T, ByVal logName As String) As ILog
            Return Log(logName)
        End Function

        ''' <summary>
        ''' Returns the logger with the specified log name.
        ''' </summary>
        ''' <param name="logName">The name of the logger to use.</param>
        ''' <returns>The logger with the specified log name.</returns>
        <Extension()> _
        Public Function Log(ByVal logName As String) As ILog
            Dim logger As ILog = Nothing
            _loggers.TryGetValue(logName, logger)
            If logger Is Nothing Then
                logger = Mercury.Mobile.Logging.Log.GetLogger(logName)
                If logger IsNot Nothing Then _
                    _loggers.TryAdd(logName, logger)
            End If
            Return logger
        End Function

    End Module

End Namespace