{"id":1029,"date":"2010-09-30T17:43:23","date_gmt":"2010-09-30T12:13:23","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1029"},"modified":"2010-09-30T17:43:23","modified_gmt":"2010-09-30T12:13:23","slug":"logging-made-easy-in-net-log-4-net","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/logging-made-easy-in-net-log-4-net\/","title":{"rendered":"Logging made easy in .NET &#8211; log 4 net"},"content":{"rendered":"<p>Logging is the part of\u00a0development\u00a0which is normally not included in Product \/ Software release but\u00a0<strong>at the same time it is necessary to create the logging mechanism in your software\u00a0module.<\/strong><\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/09\/implementl_log4net_net.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-1030\" title=\"implement log4net in .net\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/09\/implementl_log4net_net.jpg?ssl=1\" alt=\"implement log4net in .net\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<p>Without logging, how you will track<\/p>\n<ol>\n<li>run time errors and exceptions in Websites<\/li>\n<li>Status of Windows service.<\/li>\n<li>Softwares which will run in background.<\/li>\n<li>Crash reports etc.<\/li>\n<\/ol>\n<p>In this article i am going to explain that how to configure the <strong>log 4 net <\/strong>in you .net based applications.<\/p>\n<p>To start with, you have to download the log 4 net from <a title=\"Log 4 net\" href=\"http:\/\/logging.apache.org\/log4net\/\" target=\"_blank\">here<\/a>.<\/p>\n<p>After downloading, you will need to set the configurations related to Log4net. (<strong>web.config <\/strong>in case of websites else <strong>app.config<\/strong>)<\/p>\n<p>code snapshot in configuration file:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;configSections&gt;\n    &lt;section name=&quot;log4net&quot; type=&quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&quot; \/&gt;\n  &lt;\/configSections&gt;\n &lt;log4net&gt;\n    &lt;appender name=&quot;RollingLogFileAppender&quot; type=&quot;log4net.Appender.RollingFileAppender,log4net&quot;&gt;\n      &lt;file value=&quot;Logs\/Notification&quot; \/&gt;\n      &lt;appendToFile value=&quot;true&quot; \/&gt;\n      &lt;rollingStyle value=&quot;Date&quot; \/&gt;\n      &lt;StaticLogFileName value=&quot;false&quot;\/&gt;\n      &lt;datePattern value=&quot;-dd-MMM-yy'.log'&quot; \/&gt;\n      &lt;!--Roll every Day--&gt;\n      &lt;maxSizeRollBackups value=&quot;-1&quot; \/&gt;\n      &lt;!--Infinite backups--&gt;\n      &lt;maximumFileSize value=&quot;2MB&quot; \/&gt;\n      &lt;!--Maximum size of 1 file--&gt;\n      &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;\n        &lt;!-- http:\/\/logging.apache.org\/log4net \/release\/sdk\/log4net.Layout.PatternLayout.html --&gt;\n        &lt;conversionPattern value=&quot;%date{dd-MMM-yy, HH:mm:ss} - %5level &#x5B;%thread] - %message%newline &quot; \/&gt;\n      &lt;\/layout&gt;\n    &lt;\/appender&gt;\n    &lt;appender name=&quot;SmtpAppender&quot; type=&quot;log4net.Appender.SmtpAppender&quot;&gt;\n      &lt;to value=&quot;EMAIL&quot; \/&gt;\n      &lt;from value=&quot;EMAIL&quot; \/&gt;\n      &lt;subject value=&quot;Error in Component&quot; \/&gt;\n      &lt;smtpHost value=&quot;SMTP SERVER&quot; \/&gt;\n      &lt;bufferSize value=&quot;512&quot; \/&gt;\n      &lt;lossy value=&quot;true&quot; \/&gt;\n      &lt;evaluator type=&quot;log4net.Core.LevelEvaluator&quot;&gt;\n        &lt;threshold value=&quot;FATAL&quot;\/&gt;\n      &lt;\/evaluator&gt;\n      &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;\n        &lt;conversionPattern value=&quot;%newline%5level &#x5B;%thread] &#x5B;%file]- %message%newline &quot; \/&gt;\n      &lt;\/layout&gt;\n    &lt;\/appender&gt;\n    &lt;root&gt;\n      &lt;!-- Ascending Priority: Debug,Info,Warn,Error,Fatal,ALL --&gt;\n      &lt;priority value=&quot;ALL&quot; \/&gt;\n      &lt;appender-ref ref=&quot;RollingLogFileAppender&quot; \/&gt;\n      &lt;appender-ref ref=&quot;SmtpAppender&quot; \/&gt;\n    &lt;\/root&gt;\n  &lt;\/log4net&gt;\n<\/pre>\n<p>You can find the description inline in configuration.<\/p>\n<p><a title=\"Log 4 net\" href=\"http:\/\/logging.apache.org\/log4net\/release\/sdk\/log4net.Layout.PatternLayout.html\" target=\"_blank\">For more info, you can visit this site.<\/a><\/p>\n<p>To use log 4 net in code, after configuration you have to initialize the object of log 4 net like below line of code.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n private static readonly ILog logger = LogManager.GetLogger(typeof(Program));\n<\/pre>\n<p>After initialization, We have to configure the log 4 net.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nXmlConfigurator.Configure();\n<\/pre>\n<p>To log we can decide the log levels:<br \/>\nThere are five log levels:<\/p>\n<ul>\n<li><strong>Fatal (Highest Level)<\/strong><\/li>\n<li><strong>Warn<\/strong><\/li>\n<li><strong>Error<\/strong><\/li>\n<li><strong>Debug<\/strong><\/li>\n<li><strong>Info (Lowest Level)<\/strong><\/li>\n<\/ul>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n            XmlConfigurator.Configure();\n            logger.Info(new Exception(&quot;This is test Exception&quot;));\n            logger.Debug(new Exception(&quot;This is test Exception&quot;));\n            logger.Error(new Exception(&quot;This is test Exception&quot;));\n            logger.Warn(new Exception(&quot;This is test Exception&quot;));\n            logger.Fatal(new Exception(&quot;This is test Exception&quot;));\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How to use the log 4 net in .Net application. Example, demonstration and sourcecode<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[22,5],"tags":[43,51],"class_list":["post-1029","post","type-post","status-publish","format-standard","hentry","category-csharp","category-microsoft","tag-asp-net","tag-c"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":649,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/log-event-in-windows-operating-system-using-c\/","url_meta":{"origin":1029,"position":0},"title":"Log Event in Windows Operating System Using C#","author":"Jitendra","date":"July 13, 2010","format":false,"excerpt":"Tutorial on Logging Message in EventLog of Microsoft Windows.","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4769,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/event-monitoring-and-trailhead\/","url_meta":{"origin":1029,"position":1},"title":"Event Monitoring and Trailhead","author":"Jitendra","date":"August 25, 2015","format":false,"excerpt":"This is one of most awaited functionality provided by Salesforce platform recently. In many projects,\u00a0we were getting request from clients on different kind of \u00a0auditing reports like \"most used functionality by users, who is exporting reports, API call usage, link clicks\" etc. Event monitoring tool helps to provide many information\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Login by User - Event monitoring","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/08\/elf_num_logins_by_user.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/08\/elf_num_logins_by_user.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/08\/elf_num_logins_by_user.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/08\/elf_num_logins_by_user.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3150,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/merge-pdf-in-salesforce-using-java-itextpdf-and-oauth-2\/","url_meta":{"origin":1029,"position":2},"title":"Merge PDF in Salesforce Using Java, ITextPDF and OAuth 2","author":"Jitendra","date":"December 9, 2012","format":false,"excerpt":"Its long time, since i wrote any article because of my busy schedule However this time i came with advance one. In this article we are going to use the J2EE (Servlet) to Merge PDF attachment inside salesforce with the help of OAuth and ITextPDF jar file. The reason of\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Create Remote Access in Salesforce.com for OAuth 2","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/12\/Create-Remote-Access-in-Salesforce.com_.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/12\/Create-Remote-Access-in-Salesforce.com_.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/12\/Create-Remote-Access-in-Salesforce.com_.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":5602,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/tips-to-effectively-use-google-chrome-developer-tool\/","url_meta":{"origin":1029,"position":3},"title":"Tips to effectively use Google chrome developer tool","author":"Jitendra","date":"July 29, 2016","format":false,"excerpt":"While building Lightning Components in Salesforce, how to effectively use Google chrome developer tools to troubleshoot and analyze issue","rel":"","context":"In &quot;Web Technology&quot;","block_context":{"text":"Web Technology","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/"},"img":{"alt_text":"Chrome Dev tools","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/07\/Chrome-Dev-tools.png?fit=600%2C400&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/07\/Chrome-Dev-tools.png?fit=600%2C400&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/07\/Chrome-Dev-tools.png?fit=600%2C400&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":4597,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/advance-apex-debugging-in-salesforce-and-best-practices-videos\/","url_meta":{"origin":1029,"position":4},"title":"Advance Apex debugging in Salesforce and best practices &#8211; Videos","author":"Jitendra","date":"June 27, 2015","format":false,"excerpt":"How to resolve apex debug log size limit issue, Difference between Eclipse and developer console, Interactive Apex Debugging, Keyboard shortcuts for Developer console and Other best practices","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Advance Apex debugging","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":6449,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-integration-with-sql-server-integration-services\/","url_meta":{"origin":1029,"position":5},"title":"Salesforce Integration with SQL Server Integration Services","author":"Jitendra","date":"April 15, 2018","format":false,"excerpt":"Video - Connecting SQL Server Integration Services (SSIS) with Salesforce using COZYROC Connector","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/img.youtube.com\/vi\/RA5azDuubU4\/0.jpg?resize=350%2C200","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1029"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1029\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}