Nlog with derived CustomizedTarget, Write to CustomizedTarget called in same thread as logger.Error

  Kiến thức lập trình

I derived LimitedFrequencyEmailTarget from NLog MailTarget in my asp.net core application to stop send out email if there is too many email. When I tested it in visual studio, I found that the Write in derived LimitedFrequencyEmailTarget called in same thread of my logger.Error. It is sync call. Will this happen when deployed in IIS? or does it happen only in visual studio debug?

I override:
protected override void Write(AsyncLogEventInfo logEvent) { }

The following is the stack trace:

Utilities.dll!Utilities.Lib.LimitedFrequencyEmailTarget.CheckIfTooManyEmails(System.DateTime dtNow, int currentLocation) Line 136   C#
    Utilities.dll!Utilities.Lib.LimitedFrequencyEmailTarget.Write(NLog.Common.AsyncLogEventInfo logEvent) Line 94   C#
    NLog.dll!NLog.Targets.Target.WriteAsyncThreadSafe(NLog.Common.AsyncLogEventInfo logEvent) Line 605  C#
    NLog.dll!NLog.Targets.Target.WriteAsyncLogEvent(NLog.Common.AsyncLogEventInfo logEvent) Line 328    C#
    NLog.dll!NLog.LoggerImpl.WriteToTargetWithFilterChain(NLog.Targets.Target target, NLog.Filters.FilterResult result, NLog.LogEventInfo logEvent, NLog.Common.AsyncContinuation onException) Line 157 C#
    NLog.dll!NLog.LoggerImpl.Write(System.Type loggerType, NLog.Internal.TargetWithFilterChain targetsForLevel, NLog.LogEventInfo logEvent, NLog.LogFactory logFactory) Line 99 C#
    NLog.dll!NLog.Logger.WriteToTargets(System.Type wrapperType, NLog.LogEventInfo logEvent, NLog.Internal.TargetWithFilterChain targetsForLevel) Line 758  C#
    Microsoft.Extensions.Logging.dll!Microsoft.Extensions.Logging.Logger.Log.__LoggerLog|12_0<Microsoft.Extensions.Logging.FormattedLogValues>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, Microsoft.Extensions.Logging.ILogger logger, System.Exception exception, System.Func<Microsoft.Extensions.Logging.FormattedLogValues, System.Exception, string> formatter, ref System.Collections.Generic.List<System.Exception> exceptions, Microsoft.Extensions.Logging.FormattedLogValues state) Line 44    C#
    Microsoft.Extensions.Logging.dll!Microsoft.Extensions.Logging.Logger.Log<Microsoft.Extensions.Logging.FormattedLogValues>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, Microsoft.Extensions.Logging.FormattedLogValues state, System.Exception exception, System.Func<Microsoft.Extensions.Logging.FormattedLogValues, System.Exception, string> formatter) Line 26    C#
    Microsoft.Extensions.Logging.Abstractions.dll!Microsoft.Extensions.Logging.LoggerExtensions.Log(Microsoft.Extensions.Logging.ILogger logger, Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, System.Exception exception, string message, object[] args)   Unknown
    Microsoft.Extensions.Logging.Abstractions.dll!Microsoft.Extensions.Logging.LoggerExtensions.LogError(Microsoft.Extensions.Logging.ILogger logger, string message, object[] args)    Unknown
    UI.dll!UI.Controllers.EmailController.TestedWriteOneErrorLog() Line 43  C#
    [Lightweight Function]  
    Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultTypeMapper mapper, Microsoft.Extensions.Internal.ObjectMethodExecutor executor, object controller, object[] arguments) Unknown

I expected the call to logger.Error() in function EmailController.TestedWriteOneErrorLog() will return immediately and in another thread it will call LimitedFrequencyEmailTarget.Write()

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT