{"id":10092,"date":"2016-02-16T10:49:07","date_gmt":"2016-02-16T15:49:07","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=10092"},"modified":"2016-04-05T15:19:30","modified_gmt":"2016-04-05T20:19:30","slug":"log4net","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=10092","title":{"rendered":"log4net"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Download\">Download<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Use_in_Visual_Studio_Console_App\">Use in Visual Studio Console App<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Add_log4net_to_Project_References\">Add <em>log4net<\/em> to Project References<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Add_log4net_Entry_to_AssemblyInfo.cs\">Add log4net Entry to AssemblyInfo.cs<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Add_log4net_Configuration_to_App.config\">Add log4net Configuration to App.config<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Use_in_Application\">Use in Application<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Download\"><h2>Download<\/h2><\/span>\n<p>* Down from Apache log4net <a href=\"http:\/\/logging.apache.org\/log4net\/download_log4net.cgi\">download page<\/a>.<br \/>\n&#8211; I downloaded <a href=\"http:\/\/apache.spinellicreations.com\/\/logging\/log4net\/binaries\/log4net-1.2.13-bin-newkey.zip\">log4net-1.2.13-bin-newkey.zip<\/a><br \/>\n* Unzip into a local directory<\/p>\n<span id=\"Use_in_Visual_Studio_Console_App\"><h2>Use in Visual Studio Console App<\/h2><\/span>\n<span id=\"Add_log4net_to_Project_References\"><h3>Add <em>log4net<\/em> to Project References<\/h3><\/span>\n<p>* Right click <em>your project > References<\/em> folder and select <em>Add Reference&#8230;<\/em><br \/>\n* Select <em>Browse<\/em> tab and then select <em>log4net-1.2.13\\bin\\net\\4.0\\release\\log4net.dll<\/em> file<\/p>\n<span id=\"\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2014\/06\/log4net_addReferences_1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2014\/06\/log4net_addReferences_1.jpg\" alt=\"log4net_addReferences_1\" width=\"224\" height=\"250\" class=\"aligncenter size-full wp-image-10111\" \/><\/a><\/h6><\/span>\n<span id=\"_1\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2014\/06\/log4net_addReferences_2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2014\/06\/log4net_addReferences_2.jpg\" alt=\"log4net_addReferences_2\" width=\"258\" height=\"235\" class=\"aligncenter size-full wp-image-10112\" \/><\/a><\/h6><\/span>\n<span id=\"Add_log4net_Entry_to_AssemblyInfo.cs\"><h3>Add log4net Entry to AssemblyInfo.cs<\/h3><\/span>\n<p>* Open <em>your project > Properties > AssemblyInfo.cs<\/em><br \/>\n* Add to the end of the file:<\/p>\n<pre lang=\"xml\">\r\n[assembly: log4net.Config.XmlConfigurator(Watch = true)]\r\n<\/pre>\n<span id=\"Add_log4net_Configuration_to_App.config\"><h3>Add log4net Configuration to App.config<\/h3><\/span>\n<p>* Add <em>App.config<\/em> file for your project if not already exist:<br \/>\n&#8211; Right click your project folder and select <em>Add > New Item&#8230; > Application Configuration File<\/em><br \/>\n* Add log4net configuration to <em>App.config<\/em> file. (For web application, add to project <em>Web.config<\/em> file)<br \/>\n&#8211; You can find <em>Version<\/em> and <em>PublicKeyToken<\/em> values from the <em>log4net.xml<\/em> file located in the same folder as <em>log4net.dll<\/em> file.<br \/>\n&#8211; You can find sample configurations from <a href=\"http:\/\/logging.apache.org\/log4net\/release\/manual\/configuration.html\">log4net manual configuration section<\/a>:<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<configuration>\r\n  <configSections>\r\n    <section name=\"log4net\" type=\"log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" \/>\r\n  <\/configSections>\r\n\r\n<log4net>\r\n    <!-- A1 is set to be a ConsoleAppender -->\r\n    <appender name=\"A1\" type=\"log4net.Appender.ConsoleAppender\">\r\n\r\n        <!-- A1 uses PatternLayout -->\r\n        <layout type=\"log4net.Layout.PatternLayout\">\r\n            <conversionPattern value=\"%-4timestamp [%thread] %-5level %logger %ndc - %message%newline\" \/>\r\n        <\/layout>\r\n    <\/appender>\r\n    \r\n    <!-- Set root logger level to DEBUG and its only appender to A1 -->\r\n    <root>\r\n        <level value=\"DEBUG\" \/>\r\n        <appender-ref ref=\"A1\" \/>\r\n    <\/root>\r\n<\/log4net>\r\n<\/configuration>\r\n<\/pre>\n<p>* A working example:<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\"?>\r\n<configuration>\r\n  <configSections>\r\n    <section name=\"log4net\" type=\"log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\/>\r\n  <\/configSections>\r\n\r\n  <log4net>\r\n    <appender name=\"Console\" type=\"log4net.Appender.ConsoleAppender\">\r\n      <layout type=\"log4net.Layout.PatternLayout\">\r\n        <!-- Pattern to output the caller's file name and line number -->\r\n        <conversionPattern value=\"%5level [%thread] (%file:%line) - %message%newline\" \/>\r\n      <\/layout>\r\n    <\/appender>\r\n\r\n    <appender name=\"RollingFile\" type=\"log4net.Appender.RollingFileAppender\">\r\n      <file value=\"C:\\\\LOG\\\\my.log\" \/>\r\n      <appendToFile value=\"true\" \/>\r\n      <rollingStyle value=\"Size\" \/>\r\n      <maximumFileSize value=\"100MB\" \/>\r\n      <maxSizeRollBackups value=\"26\" \/>\r\n      <staticLogFileName value=\"true\" \/>\r\n      <layout type=\"log4net.Layout.PatternLayout\">\r\n        <conversionPattern value=\"%date %level %thread %logger - %message%newline\" \/>\r\n      <\/layout>\r\n    <\/appender>\r\n\r\n    <root>\r\n      <level value=\"INFO\" \/>\r\n      <appender-ref ref=\"Console\" \/>\r\n      <appender-ref ref=\"RollingFile\" \/>\r\n    <\/root>\r\n  <\/log4net>\r\n  <startup>\r\n    <supportedRuntime version=\"v4.0\" sku=\".NETFramework,Version=v4.0\"\/>\r\n  <\/startup>\r\n  <runtime>\r\n    <enforceFIPSPolicy enabled=\"false\"\/>\r\n  <\/runtime>\r\n\r\n<\/configuration>\r\n<\/pre>\n<span id=\"Use_in_Application\"><h3>Use in Application<\/h3><\/span>\n<pre lang=\"java\">\r\nusing Com.Foo;\r\n\r\n\/\/ Import log4net classes.\r\nusing log4net;\r\nusing log4net.Config;\r\n\r\npublic class MyApp \r\n{\r\n    \/\/ Define a static logger variable so that it references the\r\n    \/\/ Logger instance named \"MyApp\".\r\n    private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));\r\n\r\n    static void Main(string[] args) \r\n    {\r\n        \/\/ Set up a simple configuration that logs on the console.\r\n        BasicConfigurator.Configure();\r\n\r\n        log.Info(\"Entering application.\");\r\n        Bar bar = new Bar();\r\n        bar.DoIt();\r\n        log.Info(\"Exiting application.\");\r\n    }\r\n}\r\n<\/pre>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"http:\/\/logging.apache.org\/log4net\/\">log4net home<\/a><br \/>\n* <a href=\"http:\/\/geekswithblogs.net\/MarkPearl\/archive\/2012\/01\/30\/log4net-basics-with-a-console-application-c.aspx\">Log4Net basics with a Console Application (c#)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Download * Down from Apache log4net download page. &#8211; I downloaded log4net-1.2.13-bin-newkey.zip * Unzip into a local directory Use in Visual Studio Console App Add log4net to Project References * Right click your project > References folder and select Add &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=10092\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,494],"tags":[556,631,645],"class_list":["post-10092","post","type-post","status-publish","format-standard","hentry","category-c","category-visualstudio","tag-c","tag-logging","tag-visualstudio"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s8cRUO-log4net","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/10092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10092"}],"version-history":[{"count":12,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/10092\/revisions"}],"predecessor-version":[{"id":11392,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/10092\/revisions\/11392"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}