55using System . Text ;
66using NetCore8583 . Parse ;
77using NetCore8583 . Util ;
8- using Serilog ;
9- using Serilog . Core ;
10- using Serilog . Events ;
8+
119
1210namespace NetCore8583
1311{
@@ -37,11 +35,7 @@ public class MessageFactory<T> where T : IsoMessage
3735 /// The ISO header to be included in each message type
3836 /// </summary>
3937 private readonly Dictionary < int , string > _isoHeaders = new Dictionary < int , string > ( ) ;
40-
41- private readonly Logger _logger = new LoggerConfiguration ( ) . MinimumLevel . Debug ( )
42- . MinimumLevel . Override ( "Microsoft" , LogEventLevel . Information )
43- . Enrich . FromLogContext ( )
44- . WriteTo . Console ( ) . CreateLogger ( ) ;
38+
4539
4640 /// <summary>
4741 /// This map stores the message template for each message type.
@@ -256,12 +250,13 @@ public T CreateResponse(T request, bool copyAllFields = true)
256250 /// <summary>
257251 /// Sets the timezone for the specified FieldParseInfo, if it's needed for parsing dates.
258252 /// </summary>
259- /// <param name="messageType"></param>
260- /// <param name="field"></param>
261- /// <param name="tz"></param>
253+ /// <param name="messageType">the message type</param>
254+ /// <param name="field">the given field</param>
255+ /// <param name="tz">the timezone information</param>
256+ /// <param name="logger">a custom logger</param>
262257 public void SetTimezoneForParseGuide ( int messageType ,
263258 int field ,
264- TimeZoneInfo tz )
259+ TimeZoneInfo tz , ILogger logger = null )
265260 {
266261 var guide = ParseMap [ messageType ] ;
267262 var fpi = guide ? [ field ] ;
@@ -271,9 +266,7 @@ public void SetTimezoneForParseGuide(int messageType,
271266 return ;
272267 }
273268
274- _logger . Warning ( "Field {@Field} for message type {@MessageType} is not for dates, cannot set timezone" ,
275- field ,
276- messageType ) ;
269+ logger ? . Warning ( $ "Field { field } for message type { messageType } is not for dates, cannot set timezone") ;
277270 }
278271
279272 /// <summary>
@@ -290,10 +283,11 @@ public void SetTimezoneForParseGuide(int messageType,
290283 /// type is located, which is also the length of the ISO header.
291284 /// </param>
292285 /// <param name="binaryIsoHeader"></param>
286+ /// <param name="logger">the custom logger</param>
293287 /// <returns>The parsed message.</returns>
294288 public T ParseMessage ( sbyte [ ] buf ,
295289 int isoHeaderLength ,
296- bool binaryIsoHeader = false )
290+ bool binaryIsoHeader = false , ILogger logger = null )
297291 {
298292 var minlength = isoHeaderLength + ( UseBinaryMessages ? 2 : 4 ) +
299293 ( UseBinaryBitmap || UseBinaryMessages ? 8 : 16 ) ;
@@ -514,7 +508,7 @@ public T ParseMessage(sbyte[] buf,
514508 var index = ParseOrder [ type ] ;
515509 if ( index == null )
516510 {
517- _logger . Error (
511+ logger ? . Error (
518512 $ "ISO8583 MessageFactory has no parsing guide for message type { type : X} [{ buf . ToString ( 0 , buf . Length , _encoding ) } ]") ;
519513 throw new Exception (
520514 $ "ISO8583 MessageFactory has no parsing guide for message type { type : X} [{ buf . ToString ( 0 , buf . Length , _encoding ) } ]") ;
@@ -525,8 +519,7 @@ public T ParseMessage(sbyte[] buf,
525519 for ( var i = 1 ; i < bs . Length ; i ++ )
526520 if ( bs . Get ( i ) && ! index . Contains ( i + 1 ) )
527521 {
528- _logger . Warning ( "ISO8583 MessageFactory cannot parse field {Field}: unspecified in parsing guide" ,
529- i + 1 ) ;
522+ logger ? . Warning ( $ "ISO8583 MessageFactory cannot parse field { i + 1 } : unspecified in parsing guide") ;
530523 abandon = true ;
531524 }
532525
@@ -540,16 +533,12 @@ public T ParseMessage(sbyte[] buf,
540533 if ( ! bs . Get ( i - 1 ) ) continue ;
541534 if ( IgnoreLast && pos >= buf . Length && i == index [ ^ 1 ] )
542535 {
543- _logger . Warning ( "Field {@Index} is not really in the message even though it's in the bitmap" ,
544- i ) ;
545-
546- bs . Set ( i - 1 ,
547- false ) ;
536+ logger ? . Warning ( $ "Field { i } is not really in the message even though it's in the bitmap") ;
537+ bs . Set ( i - 1 , false ) ;
548538 }
549539 else
550540 {
551541 var decoder = fpi . Decoder ?? GetCustomField ( i ) ;
552-
553542 var val = fpi . ParseBinary ( i ,
554543 buf ,
555544 pos ,
@@ -587,11 +576,8 @@ public T ParseMessage(sbyte[] buf,
587576 if ( bs . Get ( i - 1 ) )
588577 if ( IgnoreLast && pos >= buf . Length && i == index [ ^ 1 ] )
589578 {
590- _logger . Warning (
591- "Field {@FieldId} is not really in the message even though it's in the bitmap" ,
592- i ) ;
593- bs . Set ( i - 1 ,
594- false ) ;
579+ logger ? . Warning ( $ "Field { i } is not really in the message even though it's in the bitmap") ;
580+ bs . Set ( i - 1 , false ) ;
595581 }
596582 else
597583 {
@@ -738,7 +724,7 @@ public T GetMessageTemplate(int type)
738724 }
739725
740726 public void SetParseMap ( int type ,
741- Dictionary < int , FieldParseInfo > map )
727+ Dictionary < int , FieldParseInfo > map , ILogger logger = null )
742728 {
743729 if ( ParseMap . ContainsKey ( type ) ) ParseMap [ type ] = map ;
744730 else
@@ -748,7 +734,7 @@ public void SetParseMap(int type,
748734 var index = new List < int > ( ) ;
749735 index . AddRange ( map . Keys ) ;
750736 index . Sort ( ) ;
751- _logger . Warning ( $ "ISO8583 MessageFactory adding parse map for type { type : X} with fields { index } ") ;
737+ logger ? . Warning ( $ "ISO8583 MessageFactory adding parse map for type { type : X} with fields { index } ") ;
752738
753739 if ( ParseOrder . ContainsKey ( type ) ) ParseOrder [ type ] = index ;
754740 else
0 commit comments