Exception Logging using Singleton Design Pattern


In this tutorial we will learn how to create a simple employee web application using ASP.NET MVC and we will create a custom logger library using Singleton design pattern which logs exceptions to an external file.

Text version of the video


Design Patterns Tutorial playlist

Design Patterns Text articles and slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

Nguồn: https://wijstaanvooronzegrondrechten.org

Xem thêm bài viết khác: https://wijstaanvooronzegrondrechten.org/cong-nghe


  1. Hey Avish..Thanks for this grt Video,In my standpoint of why to use an Singleton Class as design is for example if we want to handle one requirement of having say one ReportSource Class whicy emphasizes that alL the consuming client objects of this ReportSource Singleton class should be available with he updated data from DB.So to accommodate this requirement we can safely have created a Singleton Class for all incoming Client requests and it will be actually one and only one Report Object available throughout application Life Time and of course is advisable to have consuming classed implementing IDisposable pattern for memory management perpective.Thanks

  2. So what is the advantage of creating this with Singleton? We can create the log class without having to follow the Singleton Design Pattern.

  3. Just wanted to let you know that tutorials would have been much useful if MVC or any other technology is avoided and just sticked with Console app. This is because of times, I want to quickly type code myself and run.

    Otherwise, thank you very much for your work in giving us this knowledge.

  4. Great video. Just one question, will this OnException work only in the case of Singleton instance or it has nothing specific with Singleton?

  5. Hi Avish, You mentioned you will post more details on why we used singletone and other details in future but i do not see anywhere. Could you please help where you have elaborated this more. Thanks for such a good video.

  6. Good tutorial , can you also add example s of built-in .net functions which are using pattern. Example SQL adaptor is example of adaptor pattern right

  7. Can I want to add error log in globally when any one controller have an error it should be log in table or text file please mail me on
    Call. Narendra@gmail.com

  8. Thanks for the effort! I really enjoy Venkat's tutorials but I feel this one needs to be improved. The objective is to show the application of a Singleton. Any other class could have been used in this tutorial. Why use a singleton? That's the question that must be answered.

  9. Hello Avish/Venkat,
    Have a query, what will happen in below scenario:

    Suppose, there are 2 different controllers and both these controllers are instantiating "Log" instance in their own constructer concurrently and NOT simultaneously.

    How many Log instances will it create?

    Please throw some light on this.

  10. Hi Avish,

    Good work and appreciate, but I/we would need a bit dive in, when it comes to the explanatory. Ex: in previous session u have removed few of the lines due to compile error without explaining, like y it was thrown/when/where etc. Sorry to say that, Can you pls explain/focus in detail. I know you guys have enough talent. Why I am saying is, these are essential topics (design patterns). Of course every one knows 🙂

    Pls try to to catch my point its like a feedback nothing more than that ? thank you.

  11. For the most part a good presentation, however your code does NOT work the way you have presented it. File names can not have the "Exception_3/29/2018.log" format. It will give you a format Exception. When you showed the directory where the exception file was written to the Exception file name ended up something like "Exception_3-29-2018.log. What happened to the from the code. Keep up the good work but please check your work. 🙂

  12. Sir,
    I experienced an issue that prevents this example application to work properly. The problem is in the

    string logFilePath = string.Format(@"{0}{1}", AppDomain.CurrentDomain.BaseDirectory, fileName);
    line in the

    public void LogException(string message)

    Method of the Log.cs class.
    The fileName field holds the current short date in “dd/mm/yy” format in my date locale and the “/“ characters are forbidden in Windows file names, raising thus a Server Error.
    In my case it has been enough to call the Replace method of string types, in the following way fileName.Replace(“/“, “-“);
    In my case, after this amendment the application in this tutorial is working as expected.

  13. Hey Avnish. Thank you for posting video with real world example but I saw we are missing to demonstrate the actual use of singleton pattern during logging and exception. Should include how can we convert a non singleton log into singleton. But thank you so much for your efforts to teach us singleton design pattern. Keep it up buddy and wish you all the best 🙂

  14. thanks venkat and avish .u r angel for me,now i am a good programmer bcoz only ur teaching style and good explanation.thanks a lot from core of my heart.

  15. Hi Venkat & Avish

    Your videos are too good; you guys have explained many complicated concepts in such an easy way. Many thanks for sharing these video.

  16. Hi Avish,

    I feel that main intention of this video should be as showing advantages of making Logger class as singleton class instead of non-singleton class. Whole video explained about mvc exception handling and entity framework . please explain the advantages we get while making logger class as singleton and why do we need to make it as singleton or show the disadvantages of making the logger as non-singleton class. I hope that will give a clear picture.


  17. The tutorials till now were awesome and really knowledgeable. Could you please post more real projects implementation like Cache Management, Session based Shopping Card management using Singleton design pattern. Thank you very much for sharing such wonderful videos.

  18. wow … good to see that you are fan of 'Hanuman Chalisa'. I do listen these mantra pretty much every day while I am working/coding too!! Then after lunch I start soft rock collections!! Happy Coding. Thanks for awesome videos guys!!

  19. Great explanation with real project scenario!!!!!
    Eagerly waiting for your new videos..please upload it with min delay.

  20. Hi Avish,

    The tutorials till now were awesome and really knowledgeable. Could you please post more real projects implementation like Cache Management, Session based Shopping Card management using Singleton design pattern. Thank you very much for sharing such wonderful videos.


Please enter your comment!
Please enter your name here