├── .gitignore ├── LICENSE ├── README.md ├── TestingAspNetLogging.sln ├── TestingAspNetLogging ├── Controllers │ └── ValuesController.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── Services │ ├── SomeOtherService.cs │ └── SomeService.cs ├── Startup.cs ├── TestingAspNetLogging.csproj ├── appsettings.Development.json └── appsettings.json ├── TestingLogging.UnitTests ├── SomeOtherServiceDoSomething.cs ├── SomeServiceDoSomething.cs └── TestingLogging.UnitTests.csproj ├── TestingLogging.sln └── TestingLogging ├── .bowerrc ├── Controllers └── ValuesController.cs ├── Program.cs ├── Properties └── launchSettings.json ├── Services └── CourseService.cs ├── Startup.cs ├── TestingLogging.csproj ├── appsettings.Development.json └── appsettings.json /.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 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Steve Smith 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TestingLogging 2 | A sample showing how to test logging is working as expected in ASP.NET Core apps. 3 | 4 | Read the associated article on [Testing Logging in ASP.NET Core](https://ardalis.com/testing-logging-in-aspnet-core). 5 | -------------------------------------------------------------------------------- /TestingAspNetLogging.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26730.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestingAspNetLogging", "TestingAspNetLogging\TestingAspNetLogging.csproj", "{F650D1E6-F68A-40BE-860B-1CF7562EDA18}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestingLogging.UnitTests", "TestingLogging.UnitTests\TestingLogging.UnitTests.csproj", "{3E102133-FFAC-4B85-8C47-4423CC3359C7}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {F650D1E6-F68A-40BE-860B-1CF7562EDA18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {F650D1E6-F68A-40BE-860B-1CF7562EDA18}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {F650D1E6-F68A-40BE-860B-1CF7562EDA18}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {F650D1E6-F68A-40BE-860B-1CF7562EDA18}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {3E102133-FFAC-4B85-8C47-4423CC3359C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {3E102133-FFAC-4B85-8C47-4423CC3359C7}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {3E102133-FFAC-4B85-8C47-4423CC3359C7}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {3E102133-FFAC-4B85-8C47-4423CC3359C7}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {1691D5C7-D515-4402-9B20-0BA400F6D812} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /TestingAspNetLogging/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Microsoft.AspNetCore.Mvc; 3 | using TestingAspNetLogging.Services; 4 | 5 | namespace TestingAspNetLogging.Controllers 6 | { 7 | [Route("api/[controller]")] 8 | public class ValuesController : Controller 9 | { 10 | private readonly SomeService _someService; 11 | 12 | public ValuesController(SomeService someService) 13 | { 14 | _someService = someService; 15 | } 16 | // GET api/values 17 | [HttpGet] 18 | public IEnumerable Get() 19 | { 20 | return new string[] { "value1", "value2" }; 21 | } 22 | 23 | // GET api/values/5 24 | [HttpGet("{id}")] 25 | public string Get(int id) 26 | { 27 | _someService.DoSomething(id); 28 | 29 | return "value"; 30 | } 31 | 32 | // POST api/values 33 | [HttpPost] 34 | public void Post([FromBody]string value) 35 | { 36 | 37 | } 38 | 39 | // PUT api/values/5 40 | [HttpPut("{id}")] 41 | public void Put(int id, [FromBody]string value) 42 | { 43 | } 44 | 45 | // DELETE api/values/5 46 | [HttpDelete("{id}")] 47 | public void Delete(int id) 48 | { 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /TestingAspNetLogging/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore; 2 | using Microsoft.AspNetCore.Hosting; 3 | 4 | namespace TestingAspNetLogging 5 | { 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | BuildWebHost(args).Run(); 11 | } 12 | 13 | public static IWebHost BuildWebHost(string[] args) => 14 | WebHost.CreateDefaultBuilder(args) 15 | .UseStartup() 16 | .Build(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /TestingAspNetLogging/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:55937/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "TestingAspNetLogging": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:55938/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /TestingAspNetLogging/Services/SomeOtherService.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Logging; 2 | using System; 3 | 4 | namespace TestingAspNetLogging.Services 5 | { 6 | public interface ILoggerAdapter 7 | { 8 | // add just the logger methods your app uses 9 | void LogInformation(string message); 10 | void LogError(Exception ex, string message, params object[] args); 11 | } 12 | public class LoggerAdapter : ILoggerAdapter 13 | { 14 | private readonly ILogger _logger; 15 | 16 | public LoggerAdapter(ILogger logger) 17 | { 18 | _logger = logger; 19 | } 20 | 21 | public void LogError(Exception ex, string message, params object[] args) 22 | { 23 | _logger.LogError(ex, message, args); 24 | } 25 | 26 | public void LogInformation(string message) 27 | { 28 | _logger.LogInformation(message); 29 | } 30 | } 31 | public class SomeOtherService 32 | { 33 | private readonly ILoggerAdapter _logger; 34 | 35 | public SomeOtherService(ILoggerAdapter logger) 36 | { 37 | _logger = logger; 38 | } 39 | public void DoSomething(int input) 40 | { 41 | _logger.LogInformation("Doing something..."); 42 | 43 | try 44 | { 45 | // do something that might result in an exception 46 | var result = 10 / input; 47 | 48 | } 49 | catch (Exception ex) 50 | { 51 | // swallow but log the exception 52 | _logger.LogError(ex, "An error occurred doing something.", input); 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /TestingAspNetLogging/Services/SomeService.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Logging; 2 | using System; 3 | 4 | namespace TestingAspNetLogging.Services 5 | { 6 | public class SomeService 7 | { 8 | private readonly ILogger _logger; 9 | 10 | public SomeService(ILogger logger) 11 | { 12 | _logger = logger; 13 | } 14 | public void DoSomething(int input) 15 | { 16 | _logger.LogInformation("Doing something..."); 17 | 18 | try 19 | { 20 | // do something that might result in an exception 21 | var result = 10 / input; 22 | 23 | } 24 | catch (Exception ex) 25 | { 26 | // swallow but log the exception 27 | _logger.LogError(ex, "An error occurred doing something.", input); 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /TestingAspNetLogging/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.DependencyInjection; 4 | using TestingAspNetLogging.Services; 5 | 6 | namespace TestingAspNetLogging 7 | { 8 | public class Startup 9 | { 10 | // This method gets called by the runtime. Use this method to add services to the container. 11 | public void ConfigureServices(IServiceCollection services) 12 | { 13 | services.AddMvc(); 14 | services.AddTransient(); 15 | } 16 | 17 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 18 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 19 | { 20 | app.UseDeveloperExceptionPage(); 21 | app.UseMvc(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /TestingAspNetLogging/TestingAspNetLogging.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-TestingAspNetLogging-5D237FB2-6E97-485C-AF50-218F6F1CBAAF 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /TestingAspNetLogging/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TestingAspNetLogging/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TestingLogging.UnitTests/SomeOtherServiceDoSomething.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Logging; 2 | using Moq; 3 | using System; 4 | using TestingAspNetLogging.Services; 5 | using Xunit; 6 | 7 | namespace TestingLogging.UnitTests 8 | { 9 | public class SomeOtherServiceDoSomething 10 | { 11 | [Fact] 12 | public void LogsErrorWhenInputIsZero() 13 | { 14 | var mockLogger = new Mock>(); 15 | var someOtherService = new SomeOtherService(mockLogger.Object); 16 | 17 | someOtherService.DoSomething(0); 18 | 19 | mockLogger.Verify(l => l.LogError(It.IsAny(), It.IsAny(), 0)); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /TestingLogging.UnitTests/SomeServiceDoSomething.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Logging; 2 | using Microsoft.Extensions.Logging.Internal; 3 | using Moq; 4 | using System; 5 | using TestingAspNetLogging.Services; 6 | using Xunit; 7 | 8 | namespace TestingLogging.UnitTests 9 | { 10 | public class SomeServiceDoSomething 11 | { 12 | [Fact] 13 | public void LogsErrorWhenInputIsZero() 14 | { 15 | var mockLogger = new Mock>(); 16 | var someService = new SomeService(mockLogger.Object); 17 | 18 | someService.DoSomething(0); 19 | 20 | // Option 1: Try to verify the actual code that was called. 21 | // Doesn't work. 22 | mockLogger.Verify(l => l.LogError(It.IsAny(), It.IsAny(), 0)); 23 | } 24 | 25 | [Fact] 26 | public void LogsErrorWhenInputIsZeroTake2() 27 | { 28 | var mockLogger = new Mock>(); 29 | var someService = new SomeService(mockLogger.Object); 30 | 31 | someService.DoSomething(0); 32 | 33 | // Option 2: Look up what instance method the extension method actually calls: 34 | // https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Extensions.Logging.Abstractions/LoggerExtensions.cs#L342 35 | // Mock the underlying call instead. 36 | // Works but is ugly and brittle 37 | mockLogger.Verify(l => l.Log(LogLevel.Error, 0, It.IsAny(), It.IsAny(), 38 | It.IsAny>())); 39 | } 40 | 41 | [Fact] 42 | public void LogsErrorWhenInputIsZeroTake3() 43 | { 44 | var fakeLogger = new FakeLogger(); 45 | var someService = new SomeService(fakeLogger); 46 | 47 | someService.DoSomething(0); 48 | 49 | // Option 3: Create your own instance of ILogger that has a non-extension version of the method 50 | // Doesn't work, unless you change system under test to take in a FakeLogger (which is useless) 51 | Assert.NotNull(FakeLogger.ProvidedException); 52 | Assert.NotNull(FakeLogger.ProvidedMessage); 53 | } 54 | 55 | private class FakeLogger : ILogger 56 | { 57 | public static Exception ProvidedException { get; set; } 58 | public static string ProvidedMessage { get; set; } 59 | public static object[] ProvidedArgs { get; set; } 60 | public IDisposable BeginScope(TState state) 61 | { 62 | return null; 63 | } 64 | 65 | public bool IsEnabled(LogLevel logLevel) 66 | { 67 | return true; 68 | } 69 | 70 | public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) 71 | { 72 | } 73 | 74 | public void LogError(Exception ex, string message, params object[] args) 75 | { 76 | ProvidedException = ex; 77 | ProvidedMessage = message; 78 | ProvidedArgs = args; 79 | } 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /TestingLogging.UnitTests/TestingLogging.UnitTests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /TestingLogging.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26621.2 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestingLogging", "TestingLogging\TestingLogging.csproj", "{9B013D7B-C505-46B2-91A5-317B804B7388}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "..\Tests\Tests.csproj", "{76FF3ED2-E201-4670-BDCB-B74876440AA1}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {9B013D7B-C505-46B2-91A5-317B804B7388}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {9B013D7B-C505-46B2-91A5-317B804B7388}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {9B013D7B-C505-46B2-91A5-317B804B7388}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {9B013D7B-C505-46B2-91A5-317B804B7388}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {76FF3ED2-E201-4670-BDCB-B74876440AA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {76FF3ED2-E201-4670-BDCB-B74876440AA1}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {76FF3ED2-E201-4670-BDCB-B74876440AA1}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {76FF3ED2-E201-4670-BDCB-B74876440AA1}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {C71638EE-4F39-4FCA-AB7F-D80E878C8809} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /TestingLogging/.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "wwwroot/lib" 3 | } 4 | -------------------------------------------------------------------------------- /TestingLogging/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | using TestingLogging.Services; 7 | 8 | namespace TestingLogging.Controllers 9 | { 10 | [Route("api/[controller]")] 11 | public class CoursesController : Controller 12 | { 13 | private readonly CourseService _courseService; 14 | 15 | public CoursesController(CourseService courseService) 16 | { 17 | this._courseService = courseService; 18 | } 19 | // GET api/values 20 | [HttpGet] 21 | public IActionResult Get() 22 | { 23 | return Ok(_courseService.Courses); 24 | } 25 | 26 | // GET api/values/5 27 | [HttpGet("{id}")] 28 | public IActionResult Get(Guid id) 29 | { 30 | return Ok(_courseService.Courses.FirstOrDefault(c => c.Id == id)); 31 | } 32 | 33 | // POST api/values 34 | [HttpPost] 35 | public IActionResult Post([FromBody]Course course) 36 | { 37 | _courseService.Courses.Add(course); 38 | 39 | return Ok(); 40 | } 41 | 42 | // PUT api/values/5 43 | [HttpPut("{id}")] 44 | public void Put(int id, [FromBody]string value) 45 | { 46 | } 47 | 48 | // DELETE api/values/5 49 | [HttpDelete("{id}")] 50 | public void Delete(int id) 51 | { 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /TestingLogging/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace TestingLogging 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /TestingLogging/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:8079/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "TestingLogging": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "https://localhost:44301/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /TestingLogging/Services/CourseService.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Logging; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | 7 | namespace TestingLogging.Services 8 | { 9 | public class CourseService 10 | { 11 | private readonly ILogger _logger; 12 | 13 | public List Courses { get; } = new List(); 14 | 15 | public CourseService(ILogger logger) 16 | { 17 | _logger = logger; 18 | } 19 | public void Enroll(Course course, Student student) 20 | { 21 | course.EnrolledStudents.Add(student); 22 | } 23 | } 24 | 25 | public class Course 26 | { 27 | public Guid Id { get; } = Guid.NewGuid(); 28 | public string Title { get; set; } 29 | public int Capacity { get; set; } = 5; 30 | 31 | public List EnrolledStudents { get; } = new List(); 32 | } 33 | 34 | public class Student 35 | { 36 | public Guid Id { get; } = Guid.NewGuid(); 37 | public string Name { get; set; } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /TestingLogging/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Microsoft.Extensions.Logging; 10 | using Microsoft.Extensions.Options; 11 | using TestingLogging.Services; 12 | 13 | namespace TestingLogging 14 | { 15 | public class Startup 16 | { 17 | public Startup(IConfiguration configuration) 18 | { 19 | Configuration = configuration; 20 | } 21 | 22 | public IConfiguration Configuration { get; } 23 | 24 | // This method gets called by the runtime. Use this method to add services to the container. 25 | public void ConfigureServices(IServiceCollection services) 26 | { 27 | services.AddMvc(); 28 | 29 | services.AddSingleton(); 30 | } 31 | 32 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 33 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, 34 | ILoggerFactory loggerFactory, 35 | CourseService courseService) 36 | { 37 | app.UseMvc(); 38 | 39 | // set up test courses 40 | var aspnetcorequickstart = new Course() { Title = "ASP.NET Core Quick Start" }; 41 | var refactoring = new Course() { Title = "Refactoring Fundamentals" }; 42 | courseService.Courses.Add(aspnetcorequickstart); 43 | courseService.Courses.Add(refactoring); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /TestingLogging/TestingLogging.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-TestingLogging-057DD15F-8F0A-4431-8C29-AF378A525B80 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /TestingLogging/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TestingLogging/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Warning" 6 | } 7 | } 8 | } 9 | --------------------------------------------------------------------------------