The log4net Tutorial: Logging in C# (hands-on from beginner to advanced)

38
1



To sign up for announcements about new videos and exclusive discounts, fill out this form: (your email will be kept safe and you will not be spammed).

This video will teach you how to use log4net without fear. Instead of copying and pasting confusing config files that cause more problems than they solve, watch this video and you will understand how to make log4net do exactly what you want.

You will be taken through step-by-step through the following sections:

1. Getting Started – installing log4net, initial configuration and getting it running in your code.
2. .NET 4.5 Changes – What additional options you have if you are using .NET 4.5.
3. Appenders – How to log to different data repositories like SQL Server, text files and more.
4. Filters – How to log only the data you want to capture while ignoring the rest. This will cover both the simple options as well as the more advanced possibilities.
5. Log Patterns – Learn how to capture a number of different pieces of data, what the pitfalls are of some data points and how to send data to your data source.
6. Advanced Logging – Take logging to the next level by capturing custom data in your own fields. Even capture data at the time of the error without any extra configuration.
7. Wrap Up – Review what has been learned and identify how to move forward with log4net.

This video course is easy for a beginner to get started with but even if you didn’t know anything coming in, if you follow the steps outlined in the video, you will become an advanced log4net user by the end of the video. The entire video is a hands-on demonstration of not just what to do but how to do it. You will be able to follow along with the video with ease. Also provided is a link to the source code for this video. The source code has been tagged at the end of each section to indicate where you can revert to in order to see the code at any point in the video.

Course Resource:
Course code:
log4net Code Project article:
log4net homepage:

Video Quick Use Guide:
4:35 – Step 1: Add log4net package
5:25 – Step 2: Add log4net configuration
18:54 – Step 3: Add log4net assembly
20:47 – Step 4: Create log4net log reference
25:30 – Step 5: Log messages
27:56 – Creating LogHelper
32:37 – Appenders
1:04:46 – Filters
1:14:13 – Conversion pattern

Nguồn:https://wijstaanvooronzegrondrechten.org/

Xem Thêm Bài Viết Khác:https://wijstaanvooronzegrondrechten.org/cong-nghe

38 COMMENTS

  1. killed a day on various guides, but everything was very superficial, like now we’ll make config the whole end of the guide, very cool, thanks

  2. Hi Sir,
    Tutorial was exceptionally superb and well explained. Is there any way that we can get an ID in case of AdoNetAppender, so that we can pass exception id to user ??

  3. Hi Tim, Great tutorial! Thank you. I am searching for a solution to set the logfile path at runtime. Is there an easy way?

  4. Hey @IAmTimCorey,
    As of now, would you still recommend Log4net over NLog? And if so, what would be the main reason? Thanks!

  5. One nice trick I found: add <add key="log4net.Internal.Debug" value="true"/> to your appSettings Tag. Makes it a lot easier to debug eg. the ADO Appender

  6. Again – concise and consistent. Do you know that even your Code Project article is mentioned in a Pluralsight course? I got everything I need from your video so no need to watch a 3 times longer course.

  7. Hii Tim!!! I have a question "is it possibile to use log4net with rollingfileappender for the webapp hosted with medium trust"?Even After using requirePermission ="false" in configSections in my web.config , i'm getting the following error message
    "log4net:ERROR Could not create Appender [File] of type [log4net.Appender.RollingFileAppender]. Reported error follows.

    System.Security.SecurityException: Request failed.

    at log4net.Appender.RollingFileAppender.ActivateOptions()

    at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)

    The action that failed was:

    Demand

    The type of the first permission that failed was:

    System.Security.PermissionSet

    The demand was for:

    <PermissionSet class="System.Security.PermissionSet"

    version="1"

    Unrestricted="true"/>". Is there any workaround for this issue?

  8. So far very happy with what I've been learning. Although I wasn't too familiar with the SQL section I typed it in just to get familiar with it. I've no database server set up and I've never used stored procedures. It's obvious that's a whole subject in itself.

  9. I followed until 54:00 and my adoappender is not working, I have checked all the usual suspects buffer name connection string and its just doing nothing. if i attach the log4net debugger appender it shows my error but has no real feedback on whats going wrong. any advice?

  10. Another Amazing tutorial! Thank you!
    One comment, instead of Reflection or CallerFilePath, perhaps nameof would do, at least for the issue where renaming is concerned, that'll change along the class name. No?

  11. Have you ever used NLog? And if so would you recommend it over log4net? From my research it seems that even though log4net is the most popular it isn't the best option as it has performance issues compared to other logging libraries. What has been your experience?

  12. Thank you! Great tutorial. Getting the AdoNetAppender properly configured took a little effort including getting it to work with my local SQL server installation. SQL connection type and connection string values were tricky to get right depending on whether specific features were enabled or not. The samples on the Apache Log4net page and some experimentation got me to the finish line.

  13. I thought: I need now some logger. I typed: C# logging. I saw: Tim Correy log4net tutorial 1:37h. I smiled 🙂 Thank You Tim! Thanks to that tutorial I was able to find a really annoying bug in my app.

  14. 01:30 Ugly and confusing documentation is the rule, not the exception, in software . . . and hardware, too. Those of us with grey in our beards remember when this wasn't the case, when extensive printed docs were supplied with just about everything. Today, it's a casually dismissed afterthought, and customers put up with this.

LEAVE A REPLY

Please enter your comment!
Please enter your name here