jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
 
using System;
using System.Collections;
using System.IO;
 
using log4net.Core;
using log4net.Layout.Pattern;
using log4net.Util;
using log4net.Util.PatternStringConverters;
using AppDomainPatternConverter=log4net.Layout.Pattern.AppDomainPatternConverter;
using DatePatternConverter=log4net.Layout.Pattern.DatePatternConverter;
using IdentityPatternConverter=log4net.Layout.Pattern.IdentityPatternConverter;
using PropertyPatternConverter=log4net.Layout.Pattern.PropertyPatternConverter;
using UserNamePatternConverter=log4net.Layout.Pattern.UserNamePatternConverter;
using UtcDatePatternConverter=log4net.Layout.Pattern.UtcDatePatternConverter;
 
namespace log4net.Layout
{
    /// <summary>
    /// A flexible layout configurable with pattern string.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The goal of this class is to <see cref="PatternLayout.Format(TextWriter,LoggingEvent)"/> a 
    /// <see cref="LoggingEvent"/> as a string. The results
    /// depend on the <i>conversion pattern</i>.
    /// </para>
    /// <para>
    /// The conversion pattern is closely related to the conversion
    /// pattern of the printf function in C. A conversion pattern is
    /// composed of literal text and format control expressions called
    /// <i>conversion specifiers</i>.
    /// </para>
    /// <para>
    /// <i>You are free to insert any literal text within the conversion
    /// pattern.</i>
    /// </para>
    /// <para>
    /// Each conversion specifier starts with a percent sign (%) and is
    /// followed by optional <i>format modifiers</i> and a <i>conversion
    /// pattern name</i>. The conversion pattern name specifies the type of
    /// data, e.g. logger, level, date, thread name. The format
    /// modifiers control such things as field width, padding, left and
    /// right justification. The following is a simple example.
    /// </para>
    /// <para>
    /// Let the conversion pattern be <b>"%-5level [%thread]: %message%newline"</b> and assume
    /// that the log4net environment was set to use a PatternLayout. Then the
    /// statements
    /// </para>
    /// <code lang="C#">
    /// ILog log = LogManager.GetLogger(typeof(TestApp));
    /// log.Debug("Message 1");
    /// log.Warn("Message 2");   
    /// </code>
    /// <para>would yield the output</para>
    /// <code>
    /// DEBUG [main]: Message 1
    /// WARN  [main]: Message 2  
    /// </code>
    /// <para>
    /// Note that there is no explicit separator between text and
    /// conversion specifiers. The pattern parser knows when it has reached
    /// the end of a conversion specifier when it reads a conversion
    /// character. In the example above the conversion specifier
    /// <b>%-5level</b> means the level of the logging event should be left
    /// justified to a width of five characters.
    /// </para>
    /// <para>
    /// The recognized conversion pattern names are:
    /// </para>
    /// <list type="table">
    ///     <listheader>
    ///         <term>Conversion Pattern Name</term>
    ///         <description>Effect</description>
    ///     </listheader>
    ///     <item>
    ///         <term>a</term>
    ///         <description>Equivalent to <b>appdomain</b></description>
    ///     </item>
    ///     <item>
    ///         <term>appdomain</term>
    ///         <description>
    ///                Used to output the friendly name of the AppDomain where the 
    ///                logging event was generated. 
    ///         </description>
    ///     </item>
    ///     <item>
    ///         <term>aspnet-cache</term>
    ///         <description>
    ///             <para>
    ///             Used to output all cache items in the case of <b>%aspnet-cache</b> or just one named item if used as <b>%aspnet-cache{key}</b>
    ///             </para>
    ///             <para>
    ///             This pattern is not available for Compact Framework or Client Profile assemblies.
    ///             </para>
    ///         </description>
    ///     </item>
    ///     <item>
    ///         <term>aspnet-context</term>
    ///         <description>
    ///             <para>
    ///             Used to output all context items in the case of <b>%aspnet-context</b> or just one named item if used as <b>%aspnet-context{key}</b>
    ///             </para>
    ///             <para>
    ///             This pattern is not available for Compact Framework or Client Profile assemblies.
    ///             </para>
    ///         </description>
    ///     </item>
    ///     <item>
    ///         <term>aspnet-request</term>
    ///         <description>
    ///             <para>
    ///             Used to output all request parameters in the case of <b>%aspnet-request</b> or just one named param if used as <b>%aspnet-request{key}</b>
    ///             </para>
    ///             <para>
    ///             This pattern is not available for Compact Framework or Client Profile assemblies.
    ///             </para>
    ///         </description>
    ///     </item>
    ///     <item>
    ///         <term>aspnet-session</term>
    ///         <description>
    ///             <para>
    ///             Used to output all session items in the case of <b>%aspnet-session</b> or just one named item if used as <b>%aspnet-session{key}</b>
    ///             </para>
    ///             <para>
    ///             This pattern is not available for Compact Framework or Client Profile assemblies.
    ///             </para>
    ///         </description>
    ///     </item>
    ///     <item>
    ///         <term>c</term>
    ///         <description>Equivalent to <b>logger</b></description>
    ///     </item>
    ///     <item>
    ///         <term>C</term>
    ///         <description>Equivalent to <b>type</b></description>
    ///     </item>
    ///     <item>
    ///         <term>class</term>
    ///         <description>Equivalent to <b>type</b></description>
    ///     </item>
    ///     <item>
    ///         <term>d</term>
    ///         <description>Equivalent to <b>date</b></description>
    ///     </item>
    ///     <item>
    ///            <term>date</term> 
    ///            <description>
    ///             <para>
    ///             Used to output the date of the logging event in the local time zone. 
    ///             To output the date in universal time use the <c>%utcdate</c> pattern.
    ///             The date conversion 
    ///             specifier may be followed by a <i>date format specifier</i> enclosed 
    ///             between braces. For example, <b>%date{HH:mm:ss,fff}</b> or
    ///             <b>%date{dd MMM yyyy HH:mm:ss,fff}</b>.  If no date format specifier is 
    ///             given then ISO8601 format is
    ///             assumed (<see cref="log4net.DateFormatter.Iso8601DateFormatter"/>).
    ///             </para>
    ///             <para>
    ///             The date format specifier admits the same syntax as the
    ///             time pattern string of the <see cref="DateTime.ToString(string)"/>.
    ///             </para>
    ///             <para>
    ///             For better results it is recommended to use the log4net date
    ///             formatters. These can be specified using one of the strings
    ///             "ABSOLUTE", "DATE" and "ISO8601" for specifying 
    ///             <see cref="log4net.DateFormatter.AbsoluteTimeDateFormatter"/>, 
    ///             <see cref="log4net.DateFormatter.DateTimeDateFormatter"/> and respectively 
    ///             <see cref="log4net.DateFormatter.Iso8601DateFormatter"/>. For example, 
    ///             <b>%date{ISO8601}</b> or <b>%date{ABSOLUTE}</b>.
    ///             </para>
    ///             <para>
    ///             These dedicated date formatters perform significantly
    ///             better than <see cref="DateTime.ToString(string)"/>.
    ///             </para>
    ///            </description>
    ///        </item>
    ///        <item>
    ///            <term>exception</term>
    ///            <description>
    ///                <para>
    ///                Used to output the exception passed in with the log message.
    ///                </para>
    ///                <para>
    ///             If an exception object is stored in the logging event
    ///             it will be rendered into the pattern output with a
    ///             trailing newline.
    ///             If there is no exception then nothing will be output
    ///             and no trailing newline will be appended.
    ///             It is typical to put a newline before the exception
    ///             and to have the exception as the last data in the pattern.
    ///                </para>
    ///            </description>
    ///        </item>
    ///     <item>
    ///         <term>F</term>
    ///         <description>Equivalent to <b>file</b></description>
    ///     </item>
    ///        <item>
    ///            <term>file</term>
    ///            <description>
    ///                <para>
    ///                Used to output the file name where the logging request was
    ///                issued.
    ///                </para>
    ///                <para>
    ///                <b>WARNING</b> Generating caller location information is
    ///                extremely slow. Its use should be avoided unless execution speed
    ///                is not an issue.
    ///                </para>
    ///             <para>
    ///             See the note below on the availability of caller location information.
    ///             </para>
    ///            </description>
    ///        </item>
    ///        <item>
    ///            <term>identity</term>
    ///            <description>
    ///                <para>
    ///                Used to output the user name for the currently active user
    ///                (Principal.Identity.Name).
    ///                </para>
    ///                <para>
    ///                <b>WARNING</b> Generating caller information is
    ///                extremely slow. Its use should be avoided unless execution speed
    ///                is not an issue.
    ///                </para>
    ///            </description>
    ///        </item>
    ///     <item>
    ///         <term>l</term>
    ///         <description>Equivalent to <b>location</b></description>
    ///     </item>
    ///     <item>
    ///         <term>L</term>
    ///         <description>Equivalent to <b>line</b></description>
    ///     </item>
    ///        <item>
    ///            <term>location</term>
    ///            <description>
    ///             <para>
    ///             Used to output location information of the caller which generated
    ///             the logging event.
    ///             </para>
    ///             <para>
    ///             The location information depends on the CLI implementation but
    ///             usually consists of the fully qualified name of the calling
    ///             method followed by the callers source the file name and line
    ///             number between parentheses.
    ///             </para>
    ///             <para>
    ///             The location information can be very useful. However, its
    ///             generation is <b>extremely</b> slow. Its use should be avoided
    ///             unless execution speed is not an issue.
    ///             </para>
    ///             <para>
    ///             See the note below on the availability of caller location information.
    ///             </para>
    ///            </description>
    ///        </item>
    ///        <item>
    ///            <term>level</term>
    ///            <description>
    ///             <para>
    ///             Used to output the level of the logging event.
    ///             </para>
    ///            </description>
    ///        </item>
    ///        <item>
    ///            <term>line</term>
    ///            <description>
    ///                <para>
    ///                Used to output the line number from where the logging request
    ///                was issued.
    ///                </para>
    ///                <para>
    ///                <b>WARNING</b> Generating caller location information is
    ///                extremely slow. Its use should be avoided unless execution speed
    ///                is not an issue.
    ///                </para>
    ///             <para>
    ///             See the note below on the availability of caller location information.
    ///             </para>
    ///            </description>
    ///        </item>
    ///     <item>
    ///         <term>logger</term>
    ///         <description>
    ///             <para>
    ///                Used to output the logger of the logging event. The
    ///             logger conversion specifier can be optionally followed by
    ///             <i>precision specifier</i>, that is a decimal constant in
    ///             brackets.
    ///             </para>
    ///             <para>
    ///             If a precision specifier is given, then only the corresponding
    ///             number of right most components of the logger name will be
    ///             printed. By default the logger name is printed in full.
    ///             </para>
    ///             <para>
    ///             For example, for the logger name "a.b.c" the pattern
    ///             <b>%logger{2}</b> will output "b.c".
    ///             </para>
    ///         </description>
    ///     </item>
    ///     <item>
    ///         <term>m</term>
    ///         <description>Equivalent to <b>message</b></description>
    ///     </item>
    ///     <item>
    ///         <term>M</term>
    ///         <description>Equivalent to <b>method</b></description>
    ///     </item>
    ///        <item>
    ///            <term>message</term>
    ///            <description>
    ///             <para>
    ///             Used to output the application supplied message associated with 
    ///             the logging event.
    ///             </para>
    ///            </description>
    ///        </item>
    ///        <item>
    ///            <term>mdc</term>
    ///            <description>
    ///             <para>
    ///             The MDC (old name for the ThreadContext.Properties) is now part of the
    ///             combined event properties. This pattern is supported for compatibility
    ///             but is equivalent to <b>property</b>.
    ///             </para>
    ///            </description>
    ///        </item>
    ///        <item>
    ///            <term>method</term>
    ///            <description>
    ///                <para>
    ///                Used to output the method name where the logging request was
    ///                issued.
    ///                </para>
    ///                <para>
    ///                <b>WARNING</b> Generating caller location information is
    ///                extremely slow. Its use should be avoided unless execution speed
    ///                is not an issue.
    ///                </para>
    ///             <para>
    ///             See the note below on the availability of caller location information.
    ///             </para>
    ///            </description>
    ///        </item>
    ///     <item>
    ///         <term>n</term>
    ///         <description>Equivalent to <b>newline</b></description>
    ///     </item>
    ///        <item>
    ///            <term>newline</term>
    ///            <description>
    ///             <para>
    ///             Outputs the platform dependent line separator character or
    ///             characters.
    ///             </para>
    ///             <para>
    ///             This conversion pattern offers the same performance as using 
    ///             non-portable line separator strings such as    "\n", or "\r\n". 
    ///             Thus, it is the preferred way of specifying a line separator.
    ///             </para> 
    ///            </description>
    ///        </item>
    ///        <item>
    ///            <term>ndc</term>
    ///            <description>
    ///             <para>
    ///             Used to output the NDC (nested diagnostic context) associated
    ///             with the thread that generated the logging event.
    ///             </para>
    ///            </description>
    ///        </item>
    ///     <item>
    ///         <term>p</term>
    ///         <description>Equivalent to <b>level</b></description>
    ///     </item>
    ///     <item>
    ///         <term>P</term>
    ///         <description>Equivalent to <b>property</b></description>
    ///     </item>
    ///     <item>
    ///         <term>properties</term>
    ///         <description>Equivalent to <b>property</b></description>
    ///     </item>
    ///        <item>
    ///            <term>property</term>
    ///            <description>
    ///             <para>
    ///             Used to output the an event specific property. The key to 
    ///             lookup must be specified within braces and directly following the
    ///             pattern specifier, e.g. <b>%property{user}</b> would include the value
    ///             from the property that is keyed by the string 'user'. Each property value
    ///             that is to be included in the log must be specified separately.
    ///             Properties are added to events by loggers or appenders. By default 
    ///             the <c>log4net:HostName</c> property is set to the name of machine on 
    ///             which the event was originally logged.
    ///             </para>
    ///             <para>
    ///             If no key is specified, e.g. <b>%property</b> then all the keys and their
    ///             values are printed in a comma separated list.
    ///             </para>
    ///             <para>
    ///             The properties of an event are combined from a number of different
    ///             contexts. These are listed below in the order in which they are searched.
    ///             </para>
    ///             <list type="definition">
    ///                 <item>
    ///                     <term>the event properties</term>
    ///                     <description>
    ///                     The event has <see cref="LoggingEvent.Properties"/> that can be set. These 
    ///                     properties are specific to this event only.
    ///                     </description>
    ///                 </item>
    ///                 <item>
    ///                     <term>the thread properties</term>
    ///                     <description>
    ///                     The <see cref="ThreadContext.Properties"/> that are set on the current
    ///                     thread. These properties are shared by all events logged on this thread.
    ///                     </description>
    ///                 </item>
    ///                 <item>
    ///                     <term>the global properties</term>
    ///                     <description>
    ///                     The <see cref="GlobalContext.Properties"/> that are set globally. These 
    ///                     properties are shared by all the threads in the AppDomain.
    ///                     </description>
    ///                 </item>
    ///             </list>
    ///             
    ///            </description>
    ///        </item>
    ///     <item>
    ///         <term>r</term>
    ///         <description>Equivalent to <b>timestamp</b></description>
    ///     </item>
    ///     <item>
    ///            <term>stacktrace</term> 
    ///            <description>
    ///             <para>
    ///             Used to output the stack trace of the logging event
    ///             The stack trace level specifier may be enclosed 
    ///             between braces. For example, <b>%stacktrace{level}</b>.  
    ///             If no stack trace level specifier is given then 1 is assumed 
    ///             </para>
    ///             <para>
    ///             Output uses the format:
    ///             type3.MethodCall3 > type2.MethodCall2 > type1.MethodCall1
    ///             </para>
    ///             <para>
    ///             This pattern is not available for Compact Framework assemblies.
    ///             </para>
    ///            </description>
    ///        </item>
    ///     <item>
    ///            <term>stacktracedetail</term> 
    ///            <description>
    ///             <para>
    ///             Used to output the stack trace of the logging event
    ///             The stack trace level specifier may be enclosed 
    ///             between braces. For example, <b>%stacktracedetail{level}</b>.  
    ///             If no stack trace level specifier is given then 1 is assumed 
    ///             </para>
    ///             <para>
    ///             Output uses the format:
    ///             type3.MethodCall3(type param,...) > type2.MethodCall2(type param,...) > type1.MethodCall1(type param,...)
    ///             </para>
    ///             <para>
    ///             This pattern is not available for Compact Framework assemblies.
    ///             </para>
    ///            </description>
    ///        </item>
    ///     <item>
    ///         <term>t</term>
    ///         <description>Equivalent to <b>thread</b></description>
    ///     </item>
    ///        <item>
    ///            <term>timestamp</term>
    ///            <description>
    ///             <para>
    ///             Used to output the number of milliseconds elapsed since the start
    ///             of the application until the creation of the logging event.
    ///             </para>
    ///            </description>
    ///        </item>
    ///        <item>
    ///            <term>thread</term>
    ///            <description>
    ///             <para>
    ///             Used to output the name of the thread that generated the
    ///             logging event. Uses the thread number if no name is available.
    ///             </para>
    ///            </description>
    ///        </item>
    ///     <item>
    ///            <term>type</term> 
    ///            <description>
    ///             <para>
    ///             Used to output the fully qualified type name of the caller
    ///             issuing the logging request. This conversion specifier
    ///             can be optionally followed by <i>precision specifier</i>, that
    ///             is a decimal constant in brackets.
    ///             </para>
    ///             <para>
    ///             If a precision specifier is given, then only the corresponding
    ///             number of right most components of the class name will be
    ///             printed. By default the class name is output in fully qualified form.
    ///             </para>
    ///             <para>
    ///             For example, for the class name "log4net.Layout.PatternLayout", the
    ///             pattern <b>%type{1}</b> will output "PatternLayout".
    ///             </para>
    ///             <para>
    ///             <b>WARNING</b> Generating the caller class information is
    ///             slow. Thus, its use should be avoided unless execution speed is
    ///             not an issue.
    ///             </para>
    ///             <para>
    ///             See the note below on the availability of caller location information.
    ///             </para>
    ///            </description>
    ///     </item>
    ///     <item>
    ///         <term>u</term>
    ///         <description>Equivalent to <b>identity</b></description>
    ///     </item>
    ///        <item>
    ///            <term>username</term>
    ///            <description>
    ///                <para>
    ///                Used to output the WindowsIdentity for the currently
    ///                active user.
    ///                </para>
    ///                <para>
    ///                <b>WARNING</b> Generating caller WindowsIdentity information is
    ///                extremely slow. Its use should be avoided unless execution speed
    ///                is not an issue.
    ///                </para>
    ///            </description>
    ///        </item>
    ///     <item>
    ///            <term>utcdate</term> 
    ///            <description>
    ///             <para>
    ///             Used to output the date of the logging event in universal time. 
    ///             The date conversion 
    ///             specifier may be followed by a <i>date format specifier</i> enclosed 
    ///             between braces. For example, <b>%utcdate{HH:mm:ss,fff}</b> or
    ///             <b>%utcdate{dd MMM yyyy HH:mm:ss,fff}</b>.  If no date format specifier is 
    ///             given then ISO8601 format is
    ///             assumed (<see cref="log4net.DateFormatter.Iso8601DateFormatter"/>).
    ///             </para>
    ///             <para>
    ///             The date format specifier admits the same syntax as the
    ///             time pattern string of the <see cref="DateTime.ToString(string)"/>.
    ///             </para>
    ///             <para>
    ///             For better results it is recommended to use the log4net date
    ///             formatters. These can be specified using one of the strings
    ///             "ABSOLUTE", "DATE" and "ISO8601" for specifying 
    ///             <see cref="log4net.DateFormatter.AbsoluteTimeDateFormatter"/>, 
    ///             <see cref="log4net.DateFormatter.DateTimeDateFormatter"/> and respectively 
    ///             <see cref="log4net.DateFormatter.Iso8601DateFormatter"/>. For example, 
    ///             <b>%utcdate{ISO8601}</b> or <b>%utcdate{ABSOLUTE}</b>.
    ///             </para>
    ///             <para>
    ///             These dedicated date formatters perform significantly
    ///             better than <see cref="DateTime.ToString(string)"/>.
    ///             </para>
    ///            </description>
    ///        </item>
    ///     <item>
    ///         <term>w</term>
    ///         <description>Equivalent to <b>username</b></description>
    ///     </item>
    ///     <item>
    ///         <term>x</term>
    ///         <description>Equivalent to <b>ndc</b></description>
    ///     </item>
    ///     <item>
    ///         <term>X</term>
    ///         <description>Equivalent to <b>mdc</b></description>
    ///     </item>
    ///        <item>
    ///            <term>%</term>
    ///            <description>
    ///             <para>
    ///             The sequence %% outputs a single percent sign.
    ///             </para>
    ///            </description>
    ///        </item>
    /// </list>
    /// <para>
    /// The single letter patterns are deprecated in favor of the 
    /// longer more descriptive pattern names.
    /// </para>
    /// <para>
    /// By default the relevant information is output as is. However,
    /// with the aid of format modifiers it is possible to change the
    /// minimum field width, the maximum field width and justification.
    /// </para>
    /// <para>
    /// The optional format modifier is placed between the percent sign
    /// and the conversion pattern name.
    /// </para>
    /// <para>
    /// The first optional format modifier is the <i>left justification
    /// flag</i> which is just the minus (-) character. Then comes the
    /// optional <i>minimum field width</i> modifier. This is a decimal
    /// constant that represents the minimum number of characters to
    /// output. If the data item requires fewer characters, it is padded on
    /// either the left or the right until the minimum width is
    /// reached. The default is to pad on the left (right justify) but you
    /// can specify right padding with the left justification flag. The
    /// padding character is space. If the data item is larger than the
    /// minimum field width, the field is expanded to accommodate the
    /// data. The value is never truncated.
    /// </para>
    /// <para>
    /// This behavior can be changed using the <i>maximum field
    /// width</i> modifier which is designated by a period followed by a
    /// decimal constant. If the data item is longer than the maximum
    /// field, then the extra characters are removed from the
    /// <i>beginning</i> of the data item and not from the end. For
    /// example, it the maximum field width is eight and the data item is
    /// ten characters long, then the first two characters of the data item
    /// are dropped. This behavior deviates from the printf function in C
    /// where truncation is done from the end.
    /// </para>
    /// <para>
    /// Below are various format modifier examples for the logger
    /// conversion specifier.
    /// </para>
    /// <div class="tablediv">
    ///        <table class="dtTABLE" cellspacing="0">
    ///            <tr>
    ///                <th>Format modifier</th>
    ///                <th>left justify</th>
    ///                <th>minimum width</th>
    ///                <th>maximum width</th>
    ///                <th>comment</th>
    ///            </tr>
    ///            <tr>
    ///                <td align="center">%20logger</td>
    ///                <td align="center">false</td>
    ///                <td align="center">20</td>
    ///                <td align="center">none</td>
    ///                <td>
    ///                    <para>
    ///                    Left pad with spaces if the logger name is less than 20
    ///                    characters long.
    ///                    </para>
    ///                </td>
    ///            </tr>
    ///            <tr>
    ///                <td align="center">%-20logger</td>
    ///                <td align="center">true</td>
    ///                <td align="center">20</td>
    ///                <td align="center">none</td>
    ///                <td>
    ///                    <para>
    ///                    Right pad with spaces if the logger 
    ///                    name is less than 20 characters long.
    ///                    </para>
    ///                </td>
    ///            </tr>
    ///            <tr>
    ///                <td align="center">%.30logger</td>
    ///                <td align="center">NA</td>
    ///                <td align="center">none</td>
    ///                <td align="center">30</td>
    ///                <td>
    ///                    <para>
    ///                    Truncate from the beginning if the logger 
    ///                    name is longer than 30 characters.
    ///                    </para>
    ///                </td>
    ///            </tr>
    ///            <tr>
    ///                <td align="center"><nobr>%20.30logger</nobr></td>
    ///                <td align="center">false</td>
    ///                <td align="center">20</td>
    ///                <td align="center">30</td>
    ///                <td>
    ///                    <para>
    ///                    Left pad with spaces if the logger name is shorter than 20
    ///                    characters. However, if logger name is longer than 30 characters,
    ///                    then truncate from the beginning.
    ///                    </para>
    ///                </td>
    ///            </tr>
    ///            <tr>
    ///                <td align="center">%-20.30logger</td>
    ///                <td align="center">true</td>
    ///                <td align="center">20</td>
    ///                <td align="center">30</td>
    ///                <td>
    ///                    <para>
    ///                    Right pad with spaces if the logger name is shorter than 20
    ///                    characters. However, if logger name is longer than 30 characters,
    ///                    then truncate from the beginning.
    ///                    </para>
    ///                </td>
    ///            </tr>
    ///        </table>
    ///    </div>
    ///    <para>
    ///    <b>Note about caller location information.</b><br />
    ///    The following patterns <c>%type %file %line %method %location %class %C %F %L %l %M</c> 
    ///    all generate caller location information.
    /// Location information uses the <c>System.Diagnostics.StackTrace</c> class to generate
    /// a call stack. The caller's information is then extracted from this stack.
    /// </para>
    /// <note type="caution">
    /// <para>
    /// The <c>System.Diagnostics.StackTrace</c> class is not supported on the 
    /// .NET Compact Framework 1.0 therefore caller location information is not
    /// available on that framework.
    /// </para>
    /// </note>
    /// <note type="caution">
    /// <para>
    /// The <c>System.Diagnostics.StackTrace</c> class has this to say about Release builds:
    /// </para>
    /// <para>
    /// "StackTrace information will be most informative with Debug build configurations. 
    /// By default, Debug builds include debug symbols, while Release builds do not. The 
    /// debug symbols contain most of the file, method name, line number, and column 
    /// information used in constructing StackFrame and StackTrace objects. StackTrace 
    /// might not report as many method calls as expected, due to code transformations 
    /// that occur during optimization."
    /// </para>
    /// <para>
    /// This means that in a Release build the caller information may be incomplete or may 
    /// not exist at all! Therefore caller location information cannot be relied upon in a Release build.
    /// </para>
    /// </note>
    /// <para>
    /// Additional pattern converters may be registered with a specific <see cref="PatternLayout"/>
    /// instance using the <see cref="AddConverter(string, Type)"/> method.
    /// </para>
    /// </remarks>
    /// <example>
    /// This is a more detailed pattern.
    /// <code><b>%timestamp [%thread] %level %logger %ndc - %message%newline</b></code>
    /// </example>
    /// <example>
    /// A similar pattern except that the relative time is
    /// right padded if less than 6 digits, thread name is right padded if
    /// less than 15 characters and truncated if longer and the logger
    /// name is left padded if shorter than 30 characters and truncated if
    /// longer.
    /// <code><b>%-6timestamp [%15.15thread] %-5level %30.30logger %ndc - %message%newline</b></code>
    /// </example>
    /// <author>Nicko Cadell</author>
    /// <author>Gert Driesen</author>
    /// <author>Douglas de la Torre</author>
    /// <author>Daniel Cazzulino</author>
    public class PatternLayout : LayoutSkeleton
    {
        #region Constants
 
        /// <summary>
        /// Default pattern string for log output. 
        /// </summary>
        /// <remarks>
        /// <para>
        /// Default pattern string for log output. 
        /// Currently set to the string <b>"%message%newline"</b> 
        /// which just prints the application supplied message. 
        /// </para>
        /// </remarks>
        public const string DefaultConversionPattern ="%message%newline";
 
        /// <summary>
        /// A detailed conversion pattern
        /// </summary>
        /// <remarks>
        /// <para>
        /// A conversion pattern which includes Time, Thread, Logger, and Nested Context.
        /// Current value is <b>%timestamp [%thread] %level %logger %ndc - %message%newline</b>.
        /// </para>
        /// </remarks>
        public const string DetailConversionPattern = "%timestamp [%thread] %level %logger %ndc - %message%newline";
 
        #endregion
 
        #region Static Fields
 
        /// <summary>
        /// Internal map of converter identifiers to converter types.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This static map is overridden by the m_converterRegistry instance map
        /// </para>
        /// </remarks>
        private static Hashtable s_globalRulesRegistry;
 
        #endregion Static Fields
 
        #region Member Variables
    
        /// <summary>
        /// the pattern
        /// </summary>
        private string m_pattern;
  
        /// <summary>
        /// the head of the pattern converter chain
        /// </summary>
        private PatternConverter m_head;
 
        /// <summary>
        /// patterns defined on this PatternLayout only
        /// </summary>
        private Hashtable m_instanceRulesRegistry = new Hashtable();
 
        #endregion
 
        #region Static Constructor
 
        /// <summary>
        /// Initialize the global registry
        /// </summary>
        /// <remarks>
        /// <para>
        /// Defines the builtin global rules.
        /// </para>
        /// </remarks>
        static PatternLayout()
        {
            s_globalRulesRegistry = new Hashtable(45);
 
            s_globalRulesRegistry.Add("literal", typeof(LiteralPatternConverter));
            s_globalRulesRegistry.Add("newline", typeof(NewLinePatternConverter));
            s_globalRulesRegistry.Add("n", typeof(NewLinePatternConverter));
 
// .NET Compact Framework 1.0 has no support for ASP.NET
// SSCLI 1.0 has no support for ASP.NET
#if !NETCF && !SSCLI && !CLIENT_PROFILE
            s_globalRulesRegistry.Add("aspnet-cache", typeof(AspNetCachePatternConverter));
            s_globalRulesRegistry.Add("aspnet-context", typeof(AspNetContextPatternConverter));
            s_globalRulesRegistry.Add("aspnet-request", typeof(AspNetRequestPatternConverter));
            s_globalRulesRegistry.Add("aspnet-session", typeof(AspNetSessionPatternConverter));
#endif
 
            s_globalRulesRegistry.Add("c", typeof(LoggerPatternConverter));
            s_globalRulesRegistry.Add("logger", typeof(LoggerPatternConverter));
 
            s_globalRulesRegistry.Add("C", typeof(TypeNamePatternConverter));
            s_globalRulesRegistry.Add("class", typeof(TypeNamePatternConverter));
            s_globalRulesRegistry.Add("type", typeof(TypeNamePatternConverter));
 
            s_globalRulesRegistry.Add("d", typeof(DatePatternConverter));
            s_globalRulesRegistry.Add("date", typeof(DatePatternConverter));
 
            s_globalRulesRegistry.Add("exception", typeof(ExceptionPatternConverter));
 
            s_globalRulesRegistry.Add("F", typeof(FileLocationPatternConverter));
            s_globalRulesRegistry.Add("file", typeof(FileLocationPatternConverter));
 
            s_globalRulesRegistry.Add("l", typeof(FullLocationPatternConverter));
            s_globalRulesRegistry.Add("location", typeof(FullLocationPatternConverter));
 
            s_globalRulesRegistry.Add("L", typeof(LineLocationPatternConverter));
            s_globalRulesRegistry.Add("line", typeof(LineLocationPatternConverter));
 
            s_globalRulesRegistry.Add("m", typeof(MessagePatternConverter));
            s_globalRulesRegistry.Add("message", typeof(MessagePatternConverter));
 
            s_globalRulesRegistry.Add("M", typeof(MethodLocationPatternConverter));
            s_globalRulesRegistry.Add("method", typeof(MethodLocationPatternConverter));
 
            s_globalRulesRegistry.Add("p", typeof(LevelPatternConverter));
            s_globalRulesRegistry.Add("level", typeof(LevelPatternConverter));
 
            s_globalRulesRegistry.Add("P", typeof(PropertyPatternConverter));
            s_globalRulesRegistry.Add("property", typeof(PropertyPatternConverter));
            s_globalRulesRegistry.Add("properties", typeof(PropertyPatternConverter));
 
            s_globalRulesRegistry.Add("r", typeof(RelativeTimePatternConverter));
            s_globalRulesRegistry.Add("timestamp", typeof(RelativeTimePatternConverter));
            
#if !NETCF
            s_globalRulesRegistry.Add("stacktrace", typeof(StackTracePatternConverter));
            s_globalRulesRegistry.Add("stacktracedetail", typeof(StackTraceDetailPatternConverter));
#endif
 
            s_globalRulesRegistry.Add("t", typeof(ThreadPatternConverter));
            s_globalRulesRegistry.Add("thread", typeof(ThreadPatternConverter));
 
            // For backwards compatibility the NDC patterns
            s_globalRulesRegistry.Add("x", typeof(NdcPatternConverter));
            s_globalRulesRegistry.Add("ndc", typeof(NdcPatternConverter));
 
            // For backwards compatibility the MDC patterns just do a property lookup
            s_globalRulesRegistry.Add("X", typeof(PropertyPatternConverter));
            s_globalRulesRegistry.Add("mdc", typeof(PropertyPatternConverter));
 
            s_globalRulesRegistry.Add("a", typeof(AppDomainPatternConverter));
            s_globalRulesRegistry.Add("appdomain", typeof(AppDomainPatternConverter));
 
            s_globalRulesRegistry.Add("u", typeof(IdentityPatternConverter));
            s_globalRulesRegistry.Add("identity", typeof(IdentityPatternConverter));
 
            s_globalRulesRegistry.Add("utcdate", typeof(UtcDatePatternConverter));
            s_globalRulesRegistry.Add("utcDate", typeof(UtcDatePatternConverter));
            s_globalRulesRegistry.Add("UtcDate", typeof(UtcDatePatternConverter));
 
            s_globalRulesRegistry.Add("w", typeof(UserNamePatternConverter));
            s_globalRulesRegistry.Add("username", typeof(UserNamePatternConverter));
        }
 
        #endregion Static Constructor
 
        #region Constructors
 
        /// <summary>
        /// Constructs a PatternLayout using the DefaultConversionPattern
        /// </summary>
        /// <remarks>
        /// <para>
        /// The default pattern just produces the application supplied message.
        /// </para>
        /// <para>
        /// Note to Inheritors: This constructor calls the virtual method
        /// <see cref="CreatePatternParser"/>. If you override this method be
        /// aware that it will be called before your is called constructor.
        /// </para>
        /// <para>
        /// As per the <see cref="IOptionHandler"/> contract the <see cref="ActivateOptions"/>
        /// method must be called after the properties on this object have been
        /// configured.
        /// </para>
        /// </remarks>
        public PatternLayout() : this(DefaultConversionPattern)
        {
        }
 
        /// <summary>
        /// Constructs a PatternLayout using the supplied conversion pattern
        /// </summary>
        /// <param name="pattern">the pattern to use</param>
        /// <remarks>
        /// <para>
        /// Note to Inheritors: This constructor calls the virtual method
        /// <see cref="CreatePatternParser"/>. If you override this method be
        /// aware that it will be called before your is called constructor.
        /// </para>
        /// <para>
        /// When using this constructor the <see cref="ActivateOptions"/> method 
        /// need not be called. This may not be the case when using a subclass.
        /// </para>
        /// </remarks>
        public PatternLayout(string pattern) 
        {
            // By default we do not process the exception
            IgnoresException = true;
 
            m_pattern = pattern;
            if (m_pattern == null)
            {
                m_pattern = DefaultConversionPattern;
            }
 
            ActivateOptions();
        }
 
        #endregion
  
        /// <summary>
        /// The pattern formatting string
        /// </summary>
        /// <remarks>
        /// <para>
        /// The <b>ConversionPattern</b> option. This is the string which
        /// controls formatting and consists of a mix of literal content and
        /// conversion specifiers.
        /// </para>
        /// </remarks>
        public string ConversionPattern
        {
            get { return m_pattern;    }
            set { m_pattern = value; }
        }
 
        /// <summary>
        /// Create the pattern parser instance
        /// </summary>
        /// <param name="pattern">the pattern to parse</param>
        /// <returns>The <see cref="PatternParser"/> that will format the event</returns>
        /// <remarks>
        /// <para>
        /// Creates the <see cref="PatternParser"/> used to parse the conversion string. Sets the
        /// global and instance rules on the <see cref="PatternParser"/>.
        /// </para>
        /// </remarks>
        virtual protected PatternParser CreatePatternParser(string pattern) 
        {
            PatternParser patternParser = new PatternParser(pattern);
 
            // Add all the builtin patterns
            foreach(DictionaryEntry entry in s_globalRulesRegistry)
            {
                ConverterInfo converterInfo = new ConverterInfo();
                converterInfo.Name = (string)entry.Key;
                converterInfo.Type = (Type)entry.Value;
                patternParser.PatternConverters[entry.Key] = converterInfo;
            }
            // Add the instance patterns
            foreach(DictionaryEntry entry in m_instanceRulesRegistry)
            {
                patternParser.PatternConverters[entry.Key] = entry.Value;
            }
 
            return patternParser;
        }
  
        #region Implementation of IOptionHandler
 
        /// <summary>
        /// Initialize layout options
        /// </summary>
        /// <remarks>
        /// <para>
        /// This is part of the <see cref="IOptionHandler"/> delayed object
        /// activation scheme. The <see cref="ActivateOptions"/> method must 
        /// be called on this object after the configuration properties have
        /// been set. Until <see cref="ActivateOptions"/> is called this
        /// object is in an undefined state and must not be used. 
        /// </para>
        /// <para>
        /// If any of the configuration properties are modified then 
        /// <see cref="ActivateOptions"/> must be called again.
        /// </para>
        /// </remarks>
        override public void ActivateOptions() 
        {
            m_head = CreatePatternParser(m_pattern).Parse();
 
            PatternConverter curConverter = m_head;
            while(curConverter != null)
            {
                PatternLayoutConverter layoutConverter = curConverter as PatternLayoutConverter;
                if (layoutConverter != null)
                {
                    if (!layoutConverter.IgnoresException)
                    {
                        // Found converter that handles the exception
                        this.IgnoresException = false;
 
                        break;
                    }
                }
                curConverter = curConverter.Next;
            }
        }
 
        #endregion
 
        #region Override implementation of LayoutSkeleton
 
        /// <summary>
        /// Produces a formatted string as specified by the conversion pattern.
        /// </summary>
        /// <param name="loggingEvent">the event being logged</param>
        /// <param name="writer">The TextWriter to write the formatted event to</param>
        /// <remarks>
        /// <para>
        /// Parse the <see cref="LoggingEvent"/> using the patter format
        /// specified in the <see cref="ConversionPattern"/> property.
        /// </para>
        /// </remarks>
        override public void Format(TextWriter writer, LoggingEvent loggingEvent) 
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (loggingEvent == null)
            {
                throw new ArgumentNullException("loggingEvent");
            }
 
            PatternConverter c = m_head;
 
            // loop through the chain of pattern converters
            while(c != null) 
            {
                c.Format(writer, loggingEvent);
                c = c.Next;
            }
        }
 
        #endregion
 
        /// <summary>
        /// Add a converter to this PatternLayout
        /// </summary>
        /// <param name="converterInfo">the converter info</param>
        /// <remarks>
        /// <para>
        /// This version of the method is used by the configurator.
        /// Programmatic users should use the alternative <see cref="AddConverter(string,Type)"/> method.
        /// </para>
        /// </remarks>
        public void AddConverter(ConverterInfo converterInfo)
        {
            if (converterInfo == null) throw new ArgumentNullException("converterInfo");
 
            if (!typeof(PatternConverter).IsAssignableFrom(converterInfo.Type))
            {
                throw new ArgumentException("The converter type specified [" + converterInfo.Type + "] must be a subclass of log4net.Util.PatternConverter", "converterInfo");
            }
            m_instanceRulesRegistry[converterInfo.Name] = converterInfo;
        }
 
        /// <summary>
        /// Add a converter to this PatternLayout
        /// </summary>
        /// <param name="name">the name of the conversion pattern for this converter</param>
        /// <param name="type">the type of the converter</param>
        /// <remarks>
        /// <para>
        /// Add a named pattern converter to this instance. This
        /// converter will be used in the formatting of the event.
        /// This method must be called before <see cref="ActivateOptions"/>.
        /// </para>
        /// <para>
        /// The <paramref name="type"/> specified must extend the 
        /// <see cref="PatternConverter"/> type.
        /// </para>
        /// </remarks>
        public void AddConverter(string name, Type type)
        {
            if (name == null) throw new ArgumentNullException("name");
            if (type == null) throw new ArgumentNullException("type");
 
            ConverterInfo converterInfo = new ConverterInfo();
            converterInfo.Name = name;
            converterInfo.Type = type;
 
            AddConverter(converterInfo);
        }
    }
}