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 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Copyright 2004-2006 The Apache Software Foundation
Licensed 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.
-->
<!-- Content Stylesheet for Site -->
<!-- start the processing -->
<!-- ====================================================================== -->
<!-- GENERATED FILE, DO NOT EDIT, EDIT THE XML FILE IN xdocs INSTEAD! -->
<!-- Main Page Section -->
<!-- ====================================================================== -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<meta name="author" value="Nicko Cadell">
<meta name="email" value="nicko at apache dot org">
<meta name="keywords" content="log4net configuration, log4net" />
<link href="../../css/site.css" rel="stylesheet" type="text/css"/>
<link href="../../css/style.css" rel="stylesheet" type="text/css"/>
<title>log4net - log4net Manual - Configuration</title>
</head>
<body
bgcolor="#ffffff"
text="#000000"
link="#525D76"
>
<!-- START Header table -->
<table class="banner" border="0">
<tr>
<td valign="top">
<a href="http://logging.apache.org/">
<img src="http://logging.apache.org/images/ls-logo.jpg" border="0"/>
</a>
</td>
<td align="right">
</td>
</tr>
</table>
<!-- END Header table -->
<div class="centercol">
<!-- START main table -->
<h1><a id="main"></a>log4net Manual - Configuration</h1>
<div class="i1">
<h2>Contents</h2>
<div class="i1">
<a href="#config">Configuration</a><br/>
<div class="i1">
<a href="#attributes">Configuration Attributes</a><br/>
<div class="i1">
</div>
<a href="#files">Configuration Files</a><br/>
<div class="i1">
<a href="#dot-config">.config Files</a><br/>
<div class="i1">
</div>
<a href="#reading">Reading Files Directly</a><br/>
<div class="i1">
</div>
</div>
<a href="#syntax">Configuration Syntax</a><br/>
<div class="i1">
<a href="#appenders">Appenders</a><br/>
<div class="i1">
<a href="#filters">Filters</a><br/>
<div class="i1">
</div>
<a href="#layouts">Layouts</a><br/>
<div class="i1">
</div>
</div>
<a href="#root">Root Logger</a><br/>
<div class="i1">
</div>
<a href="#loggers">Loggers</a><br/>
<div class="i1">
</div>
<a href="#renderers">Renderers</a><br/>
<div class="i1">
</div>
<a href="#param">Parameters</a><br/>
<div class="i1">
<a href="#extension">Extension Parameters</a><br/>
<div class="i1">
</div>
<a href="#new-param">Compact Parameter Syntax</a><br/>
<div class="i1">
</div>
</div>
</div>
</div>
</div>
<h2><a id="config"></a>Configuration</h2>
<div class="i1">
<p>
Inserting log requests into the application code requires a fair amount of
planning and effort. Observation shows that approximately 4 percent of code is
dedicated to logging. Consequently, even moderately sized applications will
have thousands of logging statements embedded within their code. Given their
number, it becomes imperative to manage these log statements without the need
to modify them manually.
</p>
<p>
The log4net environment is fully configurable programmatically. However, it is
far more flexible to configure log4net using configuration files. Currently,
configuration files are written in XML.
</p>
<p>
Let us give a taste of how this is done with the help of an imaginary
application
<span class="code">MyApp</span>
that uses log4net.
</p>
<div class="syntax"><pre class="code">
using Com.Foo;
// Import log4net classes.
<b>using log4net;
using log4net.Config;</b>
public class MyApp
{
// Define a static logger variable so that it references the
// Logger instance named "MyApp".
<strong>private static readonly</strong> ILog log = <strong>LogManager.GetLogger(typeof(MyApp));</strong>
static void Main(string[] args)
{
// Set up a simple configuration that logs on the console.
<strong>BasicConfigurator.Configure();</strong>
log.Info("Entering application.");
Bar bar = new Bar();
bar.DoIt();
log.Info("Exiting application.");
}
}</pre></div>
<p>
<span class="code">MyApp</span>
begins by importing log4net related classes. It then defines a static logger
variable with the name
<span class="code">MyApp</span>
which happens to be the fully qualified name of the class.
</p>
<p>
<span class="code">MyApp</span>
uses the following
<span class="code">Bar</span>
class:
</p>
<div class="syntax"><pre class="code">
// Import log4net classes.
using log4net;
<b>namespace Com.Foo</b>
{
public class Bar
{
<strong>private static readonly</strong> ILog log = <strong>LogManager.GetLogger(typeof(Bar));</strong>
public void DoIt()
{
log.Debug("Did it again!");
}
}
}</pre></div>
<p>
The invocation of the
<span class="code">BasicConfigurator.Configure()</span>
method creates a rather simple log4net setup. This method is hardwired to add
to the <i>root</i> logger a
<span class="code">ConsoleAppender</span>. The output will be formatted using a
<span class="code">PatternLayout</span>
set to the pattern
<span class="code">"%-4timestamp [%thread] %-5level %logger %ndc - %message%newline"</span>.
</p>
<p>
Note that by default, the <i>root</i> logger is assigned to
<span class="code">Level.DEBUG</span>.
</p>
<p>
The output of <span class="code">MyApp</span> is:
</p>
<div class="syntax"><pre class="code">
0 [main] INFO MyApp - Entering application.
36 [main] DEBUG Com.Foo.Bar - Did it again!
51 [main] INFO MyApp - Exiting application.</pre></div>
<p>
As a side note, let me mention that in log4net child loggers link only to their
existing ancestors. In particular, the logger named
<span class="code">Com.Foo.Bar</span>
is linked directly to the
<i>root</i>
logger, thereby circumventing the unused
<span class="code">Com</span>
or
<span class="code">Com.Foo</span>
loggers. This significantly increases performance and reduces log4net's memory
footprint.
</p>
<p>
The
<span class="code">MyApp</span>
class configures log4net by invoking
<span class="code">BasicConfigurator.Configure()</span>
method. Other classes only need to import the
<span class="code">log4net</span>
namespace, retrieve the loggers they wish to use, and log away.
</p>
<p>
The previous example always outputs the same log information. Fortunately, it
is easy to modify
<span class="code">MyApp</span>
so that the log output can be controlled at run-time. Here is a slightly
modified version.
</p>
<div class="syntax"><pre class="code">
using Com.Foo;
// Import log4net classes.
using log4net;
<b>using log4net.Config;</b>
public class MyApp
{
private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));
static void Main(string[] args)
{
// BasicConfigurator replaced with XmlConfigurator.
<strong>XmlConfigurator.Configure(new System.IO.FileInfo(args[0]));</strong>
log.Info("Entering application.");
Bar bar = new Bar();
bar.DoIt();
log.Info("Exiting application.");
}
}</pre></div>
<p>
This version of
<span class="code">MyApp</span>
instructs the
<span class="code">XmlConfigurator</span>
to parse a configuration file and set up logging accordingly. The path to the
configuration file is specified on the command line.
</p>
<p>
Here is a sample configuration file that results in exactly same output as the
previous
<span class="code">BasicConfigurator</span>
based example.
</p>
<div class="syntax"><pre class="code">
<log4net>
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" type="log4net.Appender.ConsoleAppender">
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
</log4net></pre></div>
<p>
Suppose we are no longer interested in seeing the output of any component
belonging to the
<span class="code">Com.Foo</span>
package. The following configuration file shows one possible way of achieving
this.
</p>
<div class="syntax"><pre class="code">
<log4net>
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" type="log4net.Appender.ConsoleAppender">
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<!-- Print the date in ISO 8601 format -->
<conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
<!-- Print only messages of level WARN or above in the package Com.Foo -->
<logger name="Com.Foo">
<level value="WARN" />
</logger>
</log4net></pre></div>
<p>
The output of
<span class="code">MyApp</span>
configured with this file is shown below.
</p>
<div class="syntax"><pre class="code">
2000-09-07 14:07:41,508 [main] INFO MyApp - Entering application.
2000-09-07 14:07:41,529 [main] INFO MyApp - Exiting application.</pre></div>
<p>
As the logger
<span class="code">Com.Foo.Bar</span>
does not have an assigned level, it inherits its level from
<span class="code">Com.Foo</span>, which was set to WARN in the configuration
file. The log statement from the
<span class="code">Bar.DoIt</span>
method has the level DEBUG, lower than the logger level WARN. Consequently,
<span class="code">DoIt()</span>
method's log request is suppressed.
</p>
<p>
Here is another configuration file that uses multiple appenders.
</p>
<div class="syntax"><pre class="code">
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="example.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</log4net></pre></div>
<p>
Calling the enhanced <span class="code">MyApp</span> with the this configuration file will output the
following on the console.
</p>
<div class="syntax"><pre class="code">
INFO [main] (MyApp.cs:16) - Entering application.
DEBUG [main] (Bar.cs:12) - Doing it again!
INFO [main] (MyApp.cs:19) - Exiting application.</pre></div>
<p>
In addition, as the <i>root</i> logger has been allocated a second appender, output
will also be directed to the
<span class="code">example.log</span>
file. This file will be rolled over when it reaches 100KB. When roll-over
occurs, the old version of
<span class="code">example.log</span>
is automatically moved to
<span class="code">example.log.1</span>.
</p>
<p>
Note that to obtain these different logging behaviors we did not need to
recompile code. We could just as easily have logged to an email address,
redirected all
<span class="code">Com.Foo</span>
output to an NT Event logger, or forwarded logging events to a remote log4net
server, which would log according to local server policy.
</p>
<p>
For more examples of configuring appenders using the <span class="code">XmlConfigurator</span>
see the <a href="../config-examples.html">Example Appender Configuration</a>
document.
</p>
<h3><a id="attributes"></a>Configuration Attributes</h3>
<div class="i1">
<p>
The log4net configuration can be configured using assembly-level attributes rather than
specified programmatically.
</p>
<ul>
<li>
<b>XmlConfiguratorAttribute</b>
<p>
The <span class="code">log4net.Config.XmlConfiguratorAttribute</span> Allows the
<span class="code">XmlConfigurator</span> to be configured using the following properties:
</p>
<ul>
<li>
<b>ConfigFile</b>
<p>
If specified, this is the filename of the configuration file to use with the
<span class="code">XmlConfigurator</span>. This file path is relative to the
application base directory (<span class="code">AppDomain.CurrentDomain.BaseDirectory</span>).
</p>
<p>
This property cannot be used in conjunction with the
<span class="code">ConfigFileExtension</span> property.
</p>
</li>
<li>
<b>ConfigFileExtension</b>
<p>
If specified, this is the extension for the configuration file. The assembly
file name is used as the base name with the this extension appended. For example
if the assembly is loaded from the a file <span class="code">TestApp.exe</span>
and the <span class="code">ConfigFileExtension</span> property is set to
<span class="code">log4net</span> then the configuration file name is
<span class="code">TestApp.exe.log4net</span>. This is equivalent to setting the
<span class="code">ConfigFile</span> property to <span class="code">TestApp.exe.log4net</span>.
</p>
<p>
The path to the configuration file is build by using the application base
directory (<span class="code">AppDomain.CurrentDomain.BaseDirectory</span>),
the assembly file name and the configuration file extension.
</p>
<p>
This property cannot be used in conjunction with the
<span class="code">ConfigFile</span> property.
</p>
</li>
<li>
<b>Watch</b>
<p>
If this flag is specified and set to <span class="code">true</span>
then the framework will watch the configuration file and will reload the config
each time the file is modified.
</p>
</li>
</ul>
<p>
If neither of the <span class="code">ConfigFile</span> or <span class="code">ConfigFileExtension</span>
properties are specified, the application configuration file (e.g. <span class="code">TestApp.exe.config</span>)
will be used as the log4net configuration file.
</p>
<p>
Example usage:
</p>
<div class="syntax"><pre class="code">
// Configure log4net using the .config file
<b>[assembly: log4net.Config.XmlConfigurator(Watch=true)]</b>
// This will cause log4net to look for a configuration file
// called TestApp.exe.config in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.
</pre></div>
<div class="syntax"><pre class="code">
// Configure log4net using the .log4net file
<b>[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]</b>
// This will cause log4net to look for a configuration file
// called TestApp.exe.log4net in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.
</pre></div>
<p>
This attribute may only be used once per assembly.
</p>
</li>
</ul>
<p>
Using attributes can be a clearer method for defining where the application's
configuration will be loaded from. However it is worth noting that attributes
are purely passive. They are information only. Therefore if you use configuration
attributes you must invoke log4net to allow it to read the attributes. A simple
call to <span class="code">LogManager.GetLogger</span> will cause the attributes
on the calling assembly to be read and processed. <strong>Therefore it is imperative
to make a logging call as early as possible during the application start-up, and
certainly before any external assemblies have been loaded and invoked.</strong>
</p>
</div>
<h3><a id="files"></a>Configuration Files</h3>
<div class="i1">
<p>
Typically the log4net configuration is specified using a file. This file can
be read in one of two ways:
</p>
<ul>
<li>Using the .NET <span class="code">System.Configuration</span> API</li>
<li>Reading the file contents directly</li>
</ul>
<h4><a id="dot-config"></a>.config Files</h4>
<div class="i1">
<p>
The <span class="code">System.Configuration</span> API is only available if the
configuration data is in the application's config file; the file named
<i>MyApp.exe.config</i> or <i>Web.config</i>. Because the <span class="code">System.Configuration</span>
API does not support reloading of the config file the configuration settings
cannot be watched using the <span class="code">log4net.Config.XmlConfigurator.ConfigureAndWatch</span>
methods. The main advantage of using the <span class="code">System.Configuration</span>
APIs to read the configuration data is that it requires less permissions than accessing
the configuration file directly.
</p>
<p>
The only way to configure an application using the <span class="code">System.Configuration</span>
APIs is to call the <span class="code">log4net.Config.XmlConfigurator.Configure()</span> method or
the <span class="code">log4net.Config.XmlConfigurator.Configure(ILoggerRepository)</span> method.
</p>
<p>
In order to embed the configuration data in the .config file the section name must be
identified to the .NET config file parser using a <span class="code">configSections</span> element.
The section must specify the <span class="code">log4net.Config.Log4NetConfigurationSectionHandler</span>
that will be used to parse the config section. This type must be fully assembly qualified
because it is being loaded by the .NET config file parser not by log4net. The correct
assembly name for the log4net assembly must be specified.
The following is a simple example configuration file that specifies the correct
section handler to use for the log4net section.
</p>
<div class="syntax"><pre class="code">
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration></pre></div>
<p>
In the above example the <span class="code">log4net</span> assembly is specified.
This assembly must be located where the .NET runtime can find it. For example it could
be located in the same directory as the application. If the log4net assembly is stored in
the GAC then the fully qualified assembly name must be specified including the culture,
version and public key.
</p>
<p>
When using the .config file to specify the configuration the section name and
XML element name must be <span class="code">log4net</span>.
</p>
</div>
<h4><a id="reading"></a>Reading Files Directly</h4>
<div class="i1">
<p>
The XmlConfigurator can directly read any XML file and use it to configure log4net.
This includes the application's .config file; the file named <i>MyApp.exe.config</i>
or <i>Web.config</i>. The only reason not to read the configuration file directly is if the
application does not have sufficient permissions to read the file, then the configuration
must be loaded using the .NET configuration APIs (see above).
</p>
<p>
The file to read the configuration from can be specified using any of
the <span class="code">log4net.Config.XmlConfigurator</span> methods that
accept a <span class="code">System.IO.FileInfo</span> object. Because the
file system can be monitored for file change notifications the
<span class="code">ConfigureAndWatch</span> methods can be used to monitor
the configuration file for modifications and automatically reconfigure log4net.
</p>
<p>
Additionally the <span class="code">log4net.Config.XmlConfiguratorAttribute</span>
can be used to specify the file to read the configuration from.
</p>
<p>
The configuration is read from the <span class="code">log4net</span> element
in the file. Only one <span class="code">log4net</span> element can be specified
in the file but it may be located anywhere in the XML hierarchy. For example it
may be the root element:
</p>
<div class="syntax"><pre class="code">
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net></pre></div>
<p>
Or it may be nested within other elements:
</p>
<div class="syntax"><pre class="code">
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration></pre></div>
<p>
The above example shows how the configuration data can be embedded
inside a .config file even though the file is being read directly
by log4net. An important note is that the .NET config file parser
will throw an exception if it finds an element that has not been
registered using the <span class="code">configSections</span> element.
Therefore in the above example the <span class="code">log4net</span>
section name is registered, but the type specified to handle the
section is <span class="code">System.Configuration.IgnoreSectionHandler</span>.
This is a built-in class that indicates that another method for reading
the config section will be employed.
</p>
</div>
</div>
<h3><a id="syntax"></a>Configuration Syntax</h3>
<div class="i1">
<p>
log4net includes a configuration reader that parses an XML DOM, the
<span class="code">log4net.Config.XmlConfigurator</span>. This section defines the
syntax accepted by the configurator.
</p>
<p>
This is an example of a valid XML configuration. The root element
must be <span class="code"><log4net></span>. Note that this does not mean
that this element cannot be embedded in another XML document. See the section above
on <a href="#configuration.files">Configuration Files</a> for more information
on how to embed the XmlConfigurator XML in a configuration file.
</p>
<div class="syntax"><pre class="code">
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net></pre></div>
<p>
The <span class="code"><log4net></span> element supports the following attributes:
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td>debug</td>
<td>
Optional attribute. Value must be either <span class="code">true</span> or <span class="code">false</span>.
The default value is <span class="code">false</span>. Set this attribute to <span class="code">true</span>
to enable internal log4net debugging for this configuration.
</td>
</tr>
<tr>
<td>update</td>
<td>
Optional attribute. Value must be either <span class="code">Merge</span> or <span class="code">Overwrite</span>.
The default value is <span class="code">Merge</span>. Set this attribute to <span class="code">Overwrite</span>
to reset the configuration of the repository being configured before applying this configuration.
</td>
</tr>
<tr>
<td>threshold</td>
<td>
Optional attribute. Value must be the name of a level registered on the repository.
The default value is <span class="code">ALL</span>. Set this attribute to limit the messages
that are logged across the whole repository, regardless of the logger that the message is logged to.
</td>
</tr>
</table>
</div>
<p>
The <span class="code"><log4net></span> element supports the following child elements:
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Element</th>
<th>Description</th>
</tr>
<tr>
<td>appender</td>
<td>
Zero or more elements allowed. Defines an appender.
</td>
</tr>
<tr>
<td>logger</td>
<td>
Zero or more elements allowed. Defines the configuration of a logger.
</td>
</tr>
<tr>
<td>renderer</td>
<td>
Zero or more elements allowed. Defines an object renderer.
</td>
</tr>
<tr>
<td>root</td>
<td>
Optional element, maximum of one allowed. Defines the configuration of the root logger.
</td>
</tr>
<tr>
<td>param</td>
<td>
Zero or more elements allowed. Repository specific parameters
</td>
</tr>
</table>
</div>
<h4><a id="appenders"></a>Appenders</h4>
<div class="i1">
<p>
Appenders may only be defined as child elements of the <span class="code"><log4net></span>
element. Each appender must be uniquely named. The implementing type for the appender must be specified.
</p>
<p>
This example shows an appender of type <span class="code">log4net.Appender.ConsoleAppender</span> being
defined. The appender will be known as <i>ConsoleAppender</i>.
</p>
<div class="syntax"><pre class="code">
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender></pre></div>
<p>
The <span class="code"><appender></span> element supports the following attributes:
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td>name</td>
<td>
Required attribute. Value must be a string name for this appender. The name must be unique
among all the appenders defined in this configuration file. This name is used by the
<span class="code"><appender-ref></span> element of a Logger to reference an appender.
</td>
</tr>
<tr>
<td>type</td>
<td>
Required attribute. Value must be the type name for this appender. If the appender is
not defined in the log4net assembly this type name must be fully assembly qualified.
</td>
</tr>
</table>
</div>
<p>
The <span class="code"><appender></span> element supports the following child elements:
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Element</th>
<th>Description</th>
</tr>
<tr>
<td>appender-ref</td>
<td>
Zero or more elements allowed. Allows the appender to reference other appenders.
Not supported by all appenders.
</td>
</tr>
<tr>
<td>filter</td>
<td>
Zero or more elements allowed. Defines the filters used by this appender.
</td>
</tr>
<tr>
<td>layout</td>
<td>
Optional element, maximum of one allowed. Defines the layout used by this appender.
</td>
</tr>
<tr>
<td>param</td>
<td>
Zero or more elements allowed. Appender specific parameters.
</td>
</tr>
</table>
</div>
<p>
For examples of configuring appenders see the
<a href="example-config-appender.html">Example Appender Configuration</a>
document.
</p>
<h5><a id="filters"></a>Filters</h5>
<div class="i1">
<p>
Filters elements may only be defined as children of <span class="code"><appender></span> elements.
</p>
<p>
The <span class="code"><filter></span> element supports the following attributes:
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td>type</td>
<td>
Required attribute. Value must be the type name for this filter. If the filter is
not defined in the log4net assembly this type name must be fully assembly qualified.
</td>
</tr>
</table>
</div>
<p>
The <span class="code"><filter></span> element supports the following child elements:
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Element</th>
<th>Description</th>
</tr>
<tr>
<td>param</td>
<td>
Zero or more elements allowed. Filter specific parameters.
</td>
</tr>
</table>
</div>
<p>
Filters form a chain that the event has to pass through. Any filter along the way can accept the event
and stop processing, deny the event and stop processing, or allow the event on to the next filter.
If the event gets to the end of the filter chain without being denied it is implicitly accepted and will be logged.
</p>
<div class="syntax"><pre class="code">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter></pre></div>
<p>
This filter will deny events that have a level that is lower than <span class="code">INFO</span>
or higher than <span class="code">FATAL</span>.
All events between <span class="code">INFO</span> and <span class="code">FATAL</span> will be logged.
</p>
<p>
If we want to only allow messages through that have a specific substring (e.g. 'database')
then we need to specify the following filters:
</p>
<div class="syntax"><pre class="code">
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="database" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" /></pre></div>
<p>
The first filter will look for the substring 'database' in the message text of the event.
If the text is found the filter will accept the message and filter processing will stop,
the message will be logged. If the substring is not found the event will be passed to
the next filter to process. If there is no next filter the event would be implicitly
accepted and would be logged. But because we don't want the non matching events to be
logged we need to use a <span class="code">log4net.Filter.DenyAllFilter</span>
that will just deny all events that reach it. This filter is only useful at the end of the filter chain.
</p>
<p>
If we want to allow events that have either 'database' or 'ldap' in the message text we can use the following filters:
</p>
<div class="syntax"><pre class="code">
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="database"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="ldap"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" /></pre></div>
</div>
<h5><a id="layouts"></a>Layouts</h5>
<div class="i1">
<p>
Layout elements may only be defined as children of <span class="code"><appender></span> elements.
</p>
<p>
The <span class="code"><layout></span> element supports the following attributes:
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td>type</td>
<td>
Required attribute. Value must be the type name for this layout. If the layout is
not defined in the log4net assembly this type name must be fully assembly qualified.
</td>
</tr>
</table>
</div>
<p>
The <span class="code"><layout></span> element supports the following child elements:
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Element</th>
<th>Description</th>
</tr>
<tr>
<td>param</td>
<td>
Zero or more elements allowed. Layout specific parameters.
</td>
</tr>
</table>
</div>
<p>
This example shows how to configure a layout that uses the <span class="code">log4net.Layout.PatternLayout</span>.
</p>
<div class="syntax"><pre class="code">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout></pre></div>
</div>
</div>
<h4><a id="root"></a>Root Logger</h4>
<div class="i1">
<p>
Only one root logger element may only be defined and it must be a child of <span class="code"><log4net></span> element.
The root logger is the root of the logger hierarchy. All loggers ultimately inherit from this logger.
</p>
<p>
An example root logger:
</p>
<div class="syntax"><pre class="code">
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root></pre></div>
<p>
The <span class="code"><root></span> element supports no attributes.
</p>
<p>
The <span class="code"><root></span> element supports the following child elements:
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Element</th>
<th>Description</th>
</tr>
<tr>
<td>appender-ref</td>
<td>
Zero or more elements allowed. Allows the logger to reference appenders by name.
</td>
</tr>
<tr>
<td>level</td>
<td>
Optional element, maximum of one allowed. Defines the logging level for this logger.
This logger will only accept event that are at this level or above.
</td>
</tr>
<tr>
<td>param</td>
<td>
Zero or more elements allowed. Logger specific parameters.
</td>
</tr>
</table>
</div>
</div>
<h4><a id="loggers"></a>Loggers</h4>
<div class="i1">
<p>
Logger elements may only be defined as children of the <span class="code"><log4net></span> element.
</p>
<p>
An example logger:
</p>
<div class="syntax"><pre class="code">
<logger name="LoggerName">
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</logger></pre></div>
<p>
The <span class="code"><logger></span> element supports the following attributes.
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td>name</td>
<td>
Required attribute. Value must be the name of the logger.
</td>
</tr>
<tr>
<td>additivity</td>
<td>
Optional attribute. Value may be either <span class="code">true</span> or <span class="code">false</span>.
The default value is <span class="code">true</span>. Set this attribute to <span class="code">false</span>
to prevent this logger from inheriting the appenders defined on parent loggers.
</td>
</tr>
</table>
</div>
<p>
The <span class="code"><logger></span> element supports the following child elements:
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Element</th>
<th>Description</th>
</tr>
<tr>
<td>appender-ref</td>
<td>
Zero or more elements allowed. Allows the logger to reference appenders by name.
</td>
</tr>
<tr>
<td>level</td>
<td>
Optional element, maximum of one allowed. Defines the logging level for this logger.
This logger will only accept event that are at this level or above.
</td>
</tr>
<tr>
<td>param</td>
<td>
Zero or more elements allowed. Logger specific parameters.
</td>
</tr>
</table>
</div>
</div>
<h4><a id="renderers"></a>Renderers</h4>
<div class="i1">
<p>
Renderer elements may only be defined as children of the <span class="code"><log4net></span> element.
</p>
<p>
An example renderer:
</p>
<div class="syntax"><pre class="code">
<renderer renderingClass="MyClass.MyRenderer" renderedClass="MyClass.MyFunkyObject" /></pre></div>
<p>
The <span class="code"><renderer></span> element supports the following attributes.
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td>renderingClass</td>
<td>
Required attribute. Value must be the type name for this renderer. If the type is
not defined in the log4net assembly this type name must be fully assembly qualified.
This is the type of the object that will take responsibility for rendering the
<i>renderedClass</i>.
</td>
</tr>
<tr>
<td>renderedClass</td>
<td>
Required attribute. Value must be the type name for the target type for this renderer. If the type is
not defined in the log4net assembly this type name must be fully assembly qualified.
This is the name of the type that this renderer will render.
</td>
</tr>
</table>
</div>
<p>
The <span class="code"><renderer></span> element supports no child elements.
</p>
</div>
<h4><a id="param"></a>Parameters</h4>
<div class="i1">
<p>
Parameter elements may be children of many elements. See the specific elements above for details.
</p>
<p>
An example param:
</p>
<div class="syntax"><pre class="code">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /></pre></div>
<p>
The <span class="code"><param></span> element supports the following attributes.
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td>name</td>
<td>
Required attribute. Value must be the name of the parameter to set on the parent object.
</td>
</tr>
<tr>
<td>value</td>
<td>
Optional attribute. One of <i>value</i> or <i>type</i> attributes must be specified.
The value of this attribute is a string that can be converted to the value of the
parameter.
</td>
</tr>
<tr>
<td>type</td>
<td>
Optional attribute. One of <i>value</i> or <i>type</i> attributes must be specified.
The value of this attribute is a type name to create and set as the value of the
parameter. If the type is not defined in the log4net assembly this type name must
be fully assembly qualified.
</td>
</tr>
</table>
</div>
<p>
The <span class="code"><param></span> element supports the following child elements:
</p>
<div class="table">
<table cellspacing="0">
<colgroup>
<col style="text-align: left; white-space: nowrap; width: 7em" />
<col style="text-align: left;" />
</colgroup>
<tr>
<th>Element</th>
<th>Description</th>
</tr>
<tr>
<td>param</td>
<td>
Zero or more elements allowed. Parameter specific parameters.
</td>
</tr>
</table>
</div>
<p>
An example param that uses nested param elements:
</p>
<div class="syntax"><pre class="code">
<param name="evaluator" type="log4net.spi.LevelEvaluator">
<param name="Threshold" value="WARN"/>
<param></pre></div>
<h5><a id="extension"></a>Extension Parameters</h5>
<div class="i1">
<p>
Configuration parameters map directly to writable properties on an object.
The properties available depend on the actual type of the object being
configured. The log4net SDK documentation contains the API reference for
all the components included in the log4net assembly.
</p>
<p>
For 3rd party components please see their relevant API reference for
details of the properties available.
</p>
</div>
<h5><a id="new-param"></a>Compact Parameter Syntax</h5>
<div class="i1">
<p>
All parameters may alternately be specified using the parameter name as the element name
rather than using the <i>param</i> element and <i>name</i> attribute.
</p>
<p>
For example a param:
</p>
<div class="syntax"><pre class="code">
<param name="evaluator" type="log4net.spi.LevelEvaluator">
<param name="Threshold" value="WARN"/>
<param></pre></div>
<p>
may be written as:
</p>
<div class="syntax"><pre class="code">
<evaluator type="log4net.spi.LevelEvaluator">
<threshold value="WARN"/>
<evaluator></pre></div>
</div>
</div>
</div>
</div>
</div>
<!-- FOOTER -->
<p> </p>
<div align="center"><font color="#525D76" size="-1"><em>
Copyright ©
2004-2006,
Apache Software Foundation
</em></font></div>
<!-- END main table -->
</div>
<!-- LEFT SIDE NAVIGATION -->
<!-- ============================================================ -->
<div class="leftcol">
<div class="menu_header">Apache</div>
<div class="menu_item"> <a href="http://www.apache.org">Apache Home</a>
</div>
<div class="menu_item"> <a href="http://logging.apache.org/">Logging Services</a>
</div>
<div class="menu_header">log4net Project</div>
<div class="menu_item"> <a href="../../index.html">About</a>
</div>
<div class="menu_item"> <a href="../../license.html">License</a>
</div>
<div class="menu_item"> <a href="../../support.html">Support</a>
</div>
<div class="menu_item"> <a href="../../roadmap.html">Roadmap</a>
</div>
<div class="menu_item"> <a href="../../contributing.html">Contributing</a>
</div>
<div class="menu_item"> <a href="../../history.html">History</a>
</div>
<div class="menu_item"> <a href="../../downloads.html">Downloads</a>
</div>
<div class="menu_header">log4net 1.2 Documentation</div>
<div class="menu_item"> <a href="../../release/features.html">Features</a>
</div>
<div class="menu_item"> <a href="../../release/framework-support.html">Supported Frameworks</a>
</div>
<div class="menu_item"> <a href="../../release/release-notes.html">Release Notes</a>
</div>
<div class="menu_item"> <a href="../../release/example-apps.html">Example Apps</a>
</div>
<div class="menu_item"> <a href="../../release/config-examples.html">Config Examples</a>
</div>
<div class="menu_item"> <a href="../../release/building.html">Building</a>
</div>
<div class="menu_item"> <a href="../../release/faq.html">FAQ</a>
</div>
<div class="menu_item"> <a href="../../release/howto/index.html">How Tos</a>
</div>
<div class="menu_item"> <a href="../../release/sdk/index.html">SDK Reference</a>
</div>
<div class="menu_header">log4net 1.2 Manual</div>
<div class="menu_item"> <a href="../../release/manual/introduction.html">Introduction</a>
</div>
<div class="menu_item"> <a href="../../release/manual/configuration.html">Configuration</a>
</div>
<div class="menu_item"> <a href="../../release/manual/contexts.html">Contexts</a>
</div>
<div class="menu_item"> <a href="../../release/manual/plugins.html">Plugins</a>
</div>
<div class="menu_item"> <a href="../../release/manual/repositories.html">Repositories</a>
</div>
<div class="menu_item"> <a href="../../release/manual/internals.html">Internals</a>
</div>
</div>
</body>
</html>
<!-- end the processing -->
|