├── .gitignore ├── ConsoleUI ├── App.config ├── ConsoleUI.csproj ├── Counter.cs ├── LogHelper.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── packages.config ├── Log4netTutorial.sln ├── README.md ├── Sql Scripts ├── 001 - Create Log Table.sql └── 002 - Create Log Insert Stored Procedure.sql └── packages └── repositories.config /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | [Dd]ebug/ 11 | [Dd]ebugPublic/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | bld/ 16 | [Bb]in/ 17 | [Oo]bj/ 18 | 19 | # Roslyn cache directories 20 | *.ide/ 21 | 22 | # MSTest test Results 23 | [Tt]est[Rr]esult*/ 24 | [Bb]uild[Ll]og.* 25 | 26 | #NUNIT 27 | *.VisualState.xml 28 | TestResult.xml 29 | 30 | # Build Results of an ATL Project 31 | [Dd]ebugPS/ 32 | [Rr]eleasePS/ 33 | dlldata.c 34 | 35 | *_i.c 36 | *_p.c 37 | *_i.h 38 | *.ilk 39 | *.meta 40 | *.obj 41 | *.pch 42 | *.pdb 43 | *.pgc 44 | *.pgd 45 | *.rsp 46 | *.sbr 47 | *.tlb 48 | *.tli 49 | *.tlh 50 | *.tmp 51 | *.tmp_proj 52 | *.log 53 | *.vspscc 54 | *.vssscc 55 | .builds 56 | *.pidb 57 | *.svclog 58 | *.scc 59 | *.txt 60 | Thumbs.db 61 | 62 | # Chutzpah Test files 63 | _Chutzpah* 64 | 65 | # Visual C++ cache files 66 | ipch/ 67 | *.aps 68 | *.ncb 69 | *.opensdf 70 | *.sdf 71 | *.cachefile 72 | 73 | # Visual Studio profiler 74 | *.psess 75 | *.vsp 76 | *.vspx 77 | 78 | # TFS 2012 Local Workspace 79 | $tf/ 80 | 81 | # Guidance Automation Toolkit 82 | *.gpState 83 | 84 | # ReSharper is a .NET coding add-in 85 | _ReSharper*/ 86 | *.[Rr]e[Ss]harper 87 | *.DotSettings.user 88 | 89 | # JustCode is a .NET coding addin-in 90 | .JustCode 91 | 92 | # TeamCity is a build add-in 93 | _TeamCity* 94 | 95 | # DotCover is a Code Coverage Tool 96 | *.dotCover 97 | 98 | # NCrunch 99 | _NCrunch_* 100 | .*crunch*.local.xml 101 | 102 | # MightyMoose 103 | *.mm.* 104 | AutoTest.Net/ 105 | 106 | # Web workbench (sass) 107 | .sass-cache/ 108 | 109 | # Installshield output folder 110 | [Ee]xpress/ 111 | 112 | # DocProject is a documentation generator add-in 113 | DocProject/buildhelp/ 114 | DocProject/Help/*.HxT 115 | DocProject/Help/*.HxC 116 | DocProject/Help/*.hhc 117 | DocProject/Help/*.hhk 118 | DocProject/Help/*.hhp 119 | DocProject/Help/Html2 120 | DocProject/Help/html 121 | 122 | # Click-Once directory 123 | publish/ 124 | 125 | # Publish Web Output 126 | *.[Pp]ublish.xml 127 | *.azurePubxml 128 | ## TODO: Comment the next line if you want to checkin your 129 | ## web deploy settings but do note that will include unencrypted 130 | ## passwords 131 | #*.pubxml 132 | 133 | # NuGet Packages Directory 134 | packages/* 135 | ## TODO: If the tool you use requires repositories.config 136 | ## uncomment the next line 137 | !packages/repositories.config 138 | 139 | # Enable "build/" folder in the NuGet Packages folder since 140 | # NuGet packages use it for MSBuild targets. 141 | # This line needs to be after the ignore of the build folder 142 | # (and the packages folder if the line above has been uncommented) 143 | !packages/build/ 144 | 145 | # Windows Azure Build Output 146 | csx/ 147 | *.build.csdef 148 | 149 | # Windows Store app package directory 150 | AppPackages/ 151 | 152 | # Others 153 | sql/ 154 | *.Cache 155 | ClientBin/ 156 | [Ss]tyle[Cc]op.* 157 | ~$* 158 | *~ 159 | *.dbmdl 160 | *.dbproj.schemaview 161 | *.pfx 162 | *.publishsettings 163 | node_modules/ 164 | 165 | # RIA/Silverlight projects 166 | Generated_Code/ 167 | 168 | # Backup & report files from converting an old project file 169 | # to a newer Visual Studio version. Backup files are not needed, 170 | # because we have git ;-) 171 | _UpgradeReport_Files/ 172 | Backup*/ 173 | UpgradeLog*.XML 174 | UpgradeLog*.htm 175 | 176 | # SQL Server files 177 | *.mdf 178 | *.ldf 179 | 180 | # Business Intelligence projects 181 | *.rdl.data 182 | *.bim.layout 183 | *.bim_*.settings 184 | 185 | # Microsoft Fakes 186 | FakesAssemblies/ 187 | 188 | # LightSwitch generated files 189 | GeneratedArtifacts/ 190 | _Pvt_Extensions/ 191 | ModelManifest.xml 192 | 193 | # Compiled DeGarmoLib Files 194 | Bin/DegarmoLib.dll 195 | Bin/DegarmoLib.pdb 196 | FISDashbaord/Bin/DegarmoLib.dll 197 | FISDashbaord/Bin/DegarmoLib.pdb 198 | FitIndex/Bin/DegarmoLib.dll 199 | FitIndex/Bin/DegarmoLib.pdb -------------------------------------------------------------------------------- /ConsoleUI/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 |
5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /ConsoleUI/ConsoleUI.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {12CB86E9-3A3E-4AD1-ACB5-70E616F6FAF9} 8 | Exe 9 | Properties 10 | ConsoleUI 11 | ConsoleUI 12 | v4.6 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll 38 | True 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 66 | -------------------------------------------------------------------------------- /ConsoleUI/Counter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace ConsoleUI 8 | { 9 | public class Counter 10 | { 11 | public int LoopCounter { get; set; } 12 | 13 | public override string ToString() 14 | { 15 | return LoopCounter.ToString(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ConsoleUI/LogHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.CompilerServices; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace ConsoleUI 9 | { 10 | public class LogHelper 11 | { 12 | public static log4net.ILog GetLogger([CallerFilePath]string filename = "") 13 | { 14 | return log4net.LogManager.GetLogger(filename); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ConsoleUI/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | [assembly: log4net.Config.XmlConfigurator(Watch=true)] 8 | 9 | namespace ConsoleUI 10 | { 11 | class Program 12 | { 13 | private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 14 | 15 | static void Main(string[] args) 16 | { 17 | log.Debug("Developer: Tutorial was run"); 18 | log.Info("Maintenance: water pump turned on"); 19 | log.Warn("Maintenance: the water pump is getting hot"); 20 | 21 | var i = 0; 22 | 23 | try 24 | { 25 | var x = 10 / i; 26 | } 27 | catch (DivideByZeroException ex) 28 | { 29 | log.Error("Developer: we tried to divide by zero again"); 30 | } 31 | 32 | Counter j = new Counter(); 33 | 34 | log4net.GlobalContext.Properties["Counter"] = j; 35 | 36 | for (j.LoopCounter = 0; j.LoopCounter < 5; j.LoopCounter++) 37 | { 38 | log.Fatal("This is a fatal error in the process"); 39 | } 40 | 41 | //log.Fatal("Maintenance: water pump exploded"); 42 | 43 | Console.ReadLine(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /ConsoleUI/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("ConsoleUI")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ConsoleUI")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("ecf7e478-2f45-4e50-8562-d7b5b81a4319")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /ConsoleUI/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Log4netTutorial.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleUI", "ConsoleUI\ConsoleUI.csproj", "{12CB86E9-3A3E-4AD1-ACB5-70E616F6FAF9}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {12CB86E9-3A3E-4AD1-ACB5-70E616F6FAF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {12CB86E9-3A3E-4AD1-ACB5-70E616F6FAF9}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {12CB86E9-3A3E-4AD1-ACB5-70E616F6FAF9}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {12CB86E9-3A3E-4AD1-ACB5-70E616F6FAF9}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Log4net Tutorial 2 | This repository is the sample code developed in the log4net training video [Application Logging in C#: The log4net tutorial](http://youtu.be/2lAdQ_QwNww "Application Logging in C#"). The commits in the repository have been tagged with versions that correspond to the sections of the video where that particular code was built. Below you will see what each section covers. 3 | 4 | ## Sections Covered ## 5 | ### v1.0 - Getting Started ### 6 | In this section, we start from scratch and implement logging using log4net. There isn't any complicated usage. Think of this as a step up from a simple Hello World example. 7 | 8 | ### v1.1 - .NET 4.5 Changes ### 9 | This is a brief look at how the changes in .NET 4.5 allow us more options for automatically naming our logger instance based upon the file it was created in. 10 | 11 | ### v1.2 - Appenders ### 12 | This section is all about the appenders. Where can we write data to and how do we do it for each. The major appenders are covered and the minor ones are touched upon. 13 | 14 | ### v1.3 - Filters ### 15 | In this section we look at how to use a filter to get only the messages you are really interested in logging. We cover the various options for being as specific as we want about the messages we capture. 16 | 17 | ### v1.4 - Log Patterns ### 18 | This section is designed to show you what information you can capture using the standard options provided by log4net and how you can play around with those formats for even more flexibility. 19 | 20 | ### v1.5 - Advanced Logging ### 21 | In this last section, we look at how to extend the information we can capture by providing our own custom properties. First we look at a simple variable that gets set once and does not change. After that, we look at how to add a variable once and get it to change as the value in our application changes. 22 | 23 | ## Resources ## 24 | - The training video itself: [Application Logging in C#: The log4net tutorial](http://youtu.be/2lAdQ_QwNww "Application Logging in C#") 25 | - The article it supplements: [http://www.codeproject.com/Articles/140911/log-net-Tutorial](http://www.codeproject.com/Articles/140911/log-net-Tutorial "log4net Tutorial") 26 | - The log4net homepage: [http://logging.apache.org/log4net/](http://logging.apache.org/log4net/ "log4net Homepage") 27 | 28 | **Note:** To be informed when new training resources are coming and to participate in shaping what new training resources are provided by Tim, sign up for the training announcements newsletter at [https://signup.iamtimcorey.com](https://signup.iamtimcorey.com "Training Announcements Newsletter") 29 | 30 | 31 | -------------------------------------------------------------------------------- /Sql Scripts/001 - Create Log Table.sql: -------------------------------------------------------------------------------- 1 | 2 | SET ANSI_NULLS ON 3 | GO 4 | 5 | SET QUOTED_IDENTIFIER ON 6 | GO 7 | 8 | SET ANSI_PADDING ON 9 | GO 10 | 11 | CREATE TABLE [dbo].[Logs]( 12 | [id] [int] IDENTITY(1,1) NOT NULL, 13 | [logDate] [datetime2](7) NOT NULL, 14 | [logThread] [varchar](50) NOT NULL, 15 | [logLevel] [varchar](50) NOT NULL, 16 | [logSource] [varchar](300) NOT NULL, 17 | [logMessage] [varchar](4000) NOT NULL, 18 | [exception] [varchar](4000) NULL, 19 | CONSTRAINT [PK_Logs] PRIMARY KEY CLUSTERED 20 | ( 21 | [id] ASC 22 | )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 23 | ) ON [PRIMARY] 24 | 25 | GO 26 | 27 | SET ANSI_PADDING OFF 28 | GO 29 | 30 | -------------------------------------------------------------------------------- /Sql Scripts/002 - Create Log Insert Stored Procedure.sql: -------------------------------------------------------------------------------- 1 | SET ANSI_NULLS ON 2 | GO 3 | SET QUOTED_IDENTIFIER ON 4 | GO 5 | 6 | CREATE PROCEDURE dbo.procLogs_Insert 7 | @log_date datetime2, 8 | @log_thread varchar(50), 9 | @log_level varchar(50), 10 | @log_source varchar(300), 11 | @log_message varchar(4000), 12 | @exception varchar(4000) 13 | AS 14 | BEGIN 15 | SET NOCOUNT ON; 16 | 17 | insert into dbo.Logs (logDate, logThread, logLevel, logSource, logMessage, exception) 18 | values (@log_date, @log_thread, @log_level, @log_source, @log_message, @exception) 19 | 20 | END 21 | GO 22 | -------------------------------------------------------------------------------- /packages/repositories.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | --------------------------------------------------------------------------------