{"id":1886,"date":"2023-06-15T09:11:05","date_gmt":"2023-06-15T09:11:05","guid":{"rendered":"http:\/\/waqar-arshad.com\/?p=1886"},"modified":"2023-06-15T09:11:05","modified_gmt":"2023-06-15T09:11:05","slug":"elmah-logging-fundamental","status":"publish","type":"post","link":"http:\/\/waqar-arshad.com\/index.php\/2023\/06\/15\/elmah-logging-fundamental\/","title":{"rendered":"ELMAH &#8211; Logging Fundamental"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1886\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><div class=\"pld-dislike-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-dislike-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1886\" data-trigger-type=\"dislike\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-down\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-dislike-count-wrap pld-count-wrap\"><\/span>\r\n<\/div><\/div>\n<p><strong>ELMAH<\/strong><\/p>\n\n\n\n<p>ELMAH (Error Logging Modules and Handlers) is a pluggable component that you can use to log errors without modifying the application code. The main advantage of ELMAH is it&#8217;s pluggable feature. You can easily integrate the ELMAH component in your developed application. It&#8217;s an open source project and you can customize the code according to your needs. I am not going to show you how to customize the component;\u00a0instead configuration of this component with your ASP.Net application and how you can store the error messages in SQL Server. You can also store the error information in Oracle, Access or XML. There are several options available in their website.<br><br>First task is to download the ELMAH project from following link<br><br><a href=\"http:\/\/code.google.com\/p\/elmah\/\">http:\/\/code.google.com\/p\/elmah\/<\/a><br><br>Once download is complete, create one website and add the elmah.dll reference from bin folder under the downloaded folder. All versions dll are available starting from .Net fx 1.1 to .Net fx 3.5.<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"755\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-48.png\" alt=\"\" class=\"wp-image-1887\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-48.png 630w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-48-250x300.png 250w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/figure>\n\n\n\n<p><br><br>Next, make the appropriate ELMAH entries in Web.config. In ConfigSections, add the following:<br><br>&lt;sectionGroup name=&#8221;elmah&#8221;><br>&lt;section name=&#8221;security&#8221; requirePermission=&#8221;false&#8221; type=&#8221;Elmah.SecuritySectionHandler, Elmah&#8221;\/><br>&lt;section name=&#8221;errorLog&#8221; requirePermission=&#8221;false&#8221; type=&#8221;Elmah.ErrorLogSectionHandler, Elmah&#8221;\/><br>&lt;section name=&#8221;errorMail&#8221; requirePermission=&#8221;false&#8221; type=&#8221;Elmah.ErrorMailSectionHandler, Elmah&#8221;\/><br>&lt;section name=&#8221;errorFilter&#8221; requirePermission=&#8221;false&#8221; type=&#8221;Elmah.ErrorFilterSectionHandler, Elmah&#8221;\/><br>&lt;\/sectionGroup><br><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"278\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-49.png\" alt=\"\" class=\"wp-image-1888\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-49.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-49-300x89.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-49-768x227.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><br>Add this top level element:<br><br>&lt;elmah><br>&lt;security allowRemoteAccess=&#8221;yes&#8221;\/><br>&lt;\/elmah><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"327\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-50.png\" alt=\"\" class=\"wp-image-1889\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-50.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-50-300x104.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-50-768x267.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><br><br>Under httpModules add this line:<br><br>&lt;add name=&#8221;ErrorLog&#8221; type=&#8221;Elmah.ErrorLogModule, Elmah&#8221;\/><br><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"859\" height=\"292\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-51.png\" alt=\"\" class=\"wp-image-1890\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-51.png 859w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-51-300x102.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-51-768x261.png 768w\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" \/><\/figure>\n\n\n\n<p><br><br>Under httpHandlers, add:<br><br>&lt;add verb=&#8221;POST,GET,HEAD&#8221; path=&#8221;elmah.axd&#8221; type=&#8221;Elmah.ErrorLogPageFactory, Elmah&#8221;\/><br><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"813\" height=\"205\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-52.png\" alt=\"\" class=\"wp-image-1891\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-52.png 813w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-52-300x76.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-52-768x194.png 768w\" sizes=\"auto, (max-width: 813px) 100vw, 813px\" \/><\/figure>\n\n\n\n<p><br><br>Finally, add this top level element:<br><br>&lt;location path=&#8221;elmah.axd&#8221;><br>&lt;system.web><br>&lt;authorization><br>&lt;deny users=&#8221;?&#8221;\/><br>&lt;\/authorization><br>&lt;\/system.web><br>&lt;\/location><br><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"538\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-53.png\" alt=\"\" class=\"wp-image-1892\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-53.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-53-300x172.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-53-768x440.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><br><br>That&#8217;s all. You completed the configuration to start the ELMAH.<br><br>You can now see the elmah.axd file. Open your browser and type\u00a0<a href=\"http:\/\/localhost:%3Cport%3E\/elmah.axd\">http:\/\/localhost:%3Cport%3E\/elmah.axd<\/a><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"587\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-54.png\" alt=\"\" class=\"wp-image-1893\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-54.png 940w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-54-300x187.png 300w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-54-768x480.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><br><br>You will now create some exception intentionally like divide by zero, calling the page which does not exist etc.<br><br>Create a webform and put the following code in page_load section.<br><br>int\u00a0temp = 0;<br>Response.Write(Convert.ToString(125 \/ temp));<br><br>Run the page and you will see the &#8220;yellow death screen&#8221; page.<br><br>Again run the elmah.axd page and you will find the exception details logged in the page. Try to access the page which doesn&#8217;t exist in application and the exception logged in elmah.axd page. So any application exception will log and display in elmah.axd file.<br><br>Untill this point, I did not change any application specific code. I wrote some configuration in web.config and used the elmah.dll and my exception logging starts. That is the main beauty of ELMAH.<br><br>Moving next, I will show you how to log this exception details in SQL Server database. The first task is to create the tables in the database. Open the downloaded ELMAH folder and locate the SQLServer scripts under db folder and run against your database. The ELMAH_Error table and stored procedures will be created.<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"339\" height=\"439\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-55.png\" alt=\"\" class=\"wp-image-1894\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-55.png 339w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/image-55-232x300.png 232w\" sizes=\"auto, (max-width: 339px) 100vw, 339px\" \/><\/figure>\n\n\n\n<p><br><br>Add a connection string element in web.config to point to the database.<br><br>&lt;connectionStrings><br>\u00a0\u00a0\u00a0 &lt;add\u00a0name=&#8221;ElmahConnectionString&#8221;\u00a0connectionString=&#8221;Data Source=.\\sqlexpress;Initial Catalog=EFBlogs;Integrated Security=True;Pooling=False&#8221;\/><br>\u00a0 &lt;\/connectionStrings><\/p>\n\n\n\n<p>Add following code in web.config under top level &lt;elmah&gt; tag.<\/p>\n\n\n\n<p>&lt;errorLog&nbsp;type=&#8221;Elmah.SqlErrorLog, Elmah&#8221;&nbsp;connectionStringName=&#8221;ElmahConnectionString&#8221;\/&gt;<br>That&#8217;s it. Run the pages that will raise exception and you will see the exception details starts logging in ELMAH_Error table.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ELMAH ELMAH (Error Logging Modules and Handlers) is a pluggable component that you can use to log errors without modifying the application code. The main advantage of ELMAH is it&#8217;s pluggable feature. You can easily integrate the ELMAH component in your developed application. It&#8217;s an open source project and you can customize the code according [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[12,13,11,15],"tags":[],"class_list":["post-1886","post","type-post","status-publish","format-standard","hentry","category-asp-net","category-asp-net-core","category-csharp","category-log-framework"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"admin","author_link":"http:\/\/waqar-arshad.com\/index.php\/author\/waqar_29_1\/"},"uagb_comment_info":23,"uagb_excerpt":"ELMAH ELMAH (Error Logging Modules and Handlers) is a pluggable component that you can use to log errors without modifying the application code. The main advantage of ELMAH is it&#8217;s pluggable feature. You can easily integrate the ELMAH component in your developed application. It&#8217;s an open source project and you can customize the code according&hellip;","_links":{"self":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/comments?post=1886"}],"version-history":[{"count":1,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1886\/revisions"}],"predecessor-version":[{"id":1895,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1886\/revisions\/1895"}],"wp:attachment":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/media?parent=1886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/categories?post=1886"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/tags?post=1886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}