Created
October 14, 2014 07:50
-
-
Save RhysC/2136e101fe67bbfd256d to your computer and use it in GitHub Desktop.
RestTest - sample console app for receiving HTTP Posts and logging said posts. Posting to http://localhost.fiddler:9000/api/test will allow you to see the request in fiddler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.IO; | |
| using log4net.Appender; | |
| using log4net.Core; | |
| using log4net.Layout; | |
| namespace RestTest.Logging | |
| { | |
| public static class Log4NetAppenderFactory | |
| { | |
| private static readonly PatternLayout PatternLayout = new PatternLayout("[%-5level][%date][%thread][%logger]: %message%newline"); | |
| private const long TenMb = (10 * 1024 * 1024); | |
| /// <summary> | |
| /// Creates a size based rolling file appender that is configured to fill up 31 x 10Mb log files before deleting old logs. | |
| /// Each time the log file roll overs it appends an incrementing counter onto the end of the filename. | |
| /// </summary> | |
| /// <param name="processName"></param> | |
| /// <param name="loggingPath"></param> | |
| /// <param name="desiredLoggingLevel">Log threshold level to use, defaults to ALL if not supplied.</param> | |
| /// <returns></returns> | |
| public static RollingFileAppender CreateRollingFileAppender(string processName, string loggingPath, Level desiredLoggingLevel = null) | |
| { | |
| var levelToUse = desiredLoggingLevel ?? Level.All; | |
| var fileAppender = new RollingFileAppender | |
| { | |
| File = Path.Combine(loggingPath, processName + ".log"), | |
| AppendToFile = true, | |
| ImmediateFlush = true, | |
| Layout = PatternLayout, | |
| LockingModel = new FileAppender.MinimalLock(), | |
| Threshold = levelToUse, | |
| // use size base options so log4net will auto remove old logs | |
| RollingStyle = RollingFileAppender.RollingMode.Size, | |
| MaxSizeRollBackups = 30, // gives us 31 files - the original plus 30 backups. multiply that by 10Mb then we need 310Mb of storage | |
| MaxFileSize = TenMb, | |
| StaticLogFileName = false, | |
| PreserveLogFileNameExtension = false, | |
| CountDirection = 1 | |
| }; | |
| fileAppender.ActivateOptions(); | |
| return fileAppender; | |
| } | |
| public static ColoredConsoleAppender CreateColoredConsoleAppender(Level desiredLoggingLevel = null) | |
| { | |
| var levelToUse = desiredLoggingLevel ?? Level.All; | |
| var fileAppender = new ColoredConsoleAppender | |
| { | |
| Layout = new PatternLayout("%message%newline"), | |
| Threshold = levelToUse | |
| }; | |
| fileAppender.ActivateOptions(); | |
| return fileAppender; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Reflection; | |
| using log4net; | |
| using log4net.Appender; | |
| using log4net.Core; | |
| using log4net.Repository.Hierarchy; | |
| namespace RestTest.Logging | |
| { | |
| public static class Log4NetSetUp | |
| { | |
| private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | |
| public const string LoggingPath = "C:\\Temp\\Logs"; | |
| public static void InitialiseLog4Net() | |
| { | |
| var logAppenders = new IAppender[] | |
| { | |
| Log4NetAppenderFactory.CreateColoredConsoleAppender(Level.All), | |
| Log4NetAppenderFactory.CreateRollingFileAppender("RestTest", LoggingPath, Level.All) | |
| }; | |
| var hierarchy = (Hierarchy)LogManager.GetRepository(); | |
| hierarchy.Clear(); | |
| foreach (var appender in logAppenders) | |
| { | |
| hierarchy.Root.AddAppender(appender); | |
| } | |
| hierarchy.Root.Level = Level.Debug; | |
| hierarchy.Configured = true; | |
| Log.Info("Log4Net configuration complete!"); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <packages> | |
| <package id="log4net" version="2.0.3" targetFramework="net45" /> | |
| <package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" /> | |
| <package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" /> | |
| <package id="Microsoft.AspNet.WebApi.Owin" version="5.2.2" targetFramework="net45" /> | |
| <package id="Microsoft.AspNet.WebApi.OwinSelfHost" version="5.2.2" targetFramework="net45" /> | |
| <package id="Microsoft.Owin" version="2.0.2" targetFramework="net45" /> | |
| <package id="Microsoft.Owin.Host.HttpListener" version="2.0.2" targetFramework="net45" /> | |
| <package id="Microsoft.Owin.Hosting" version="2.0.2" targetFramework="net45" /> | |
| <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" /> | |
| <package id="Owin" version="1.0" targetFramework="net45" /> | |
| </packages> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Microsoft.Owin.Hosting; | |
| using RestTest.Logging; | |
| namespace RestTest | |
| { | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| const string baseAddress = "http://localhost:9000/"; | |
| Log4NetSetUp.InitialiseLog4Net(); | |
| using (WebApp.Start<Startup>(baseAddress)) | |
| { | |
| Console.WriteLine("Running as " + baseAddress); | |
| Console.WriteLine("Logging to " + Log4NetSetUp.LoggingPath); | |
| Console.WriteLine("Send a POST request to {0}api/Test to get statrted", baseAddress); | |
| Console.WriteLine("Press enter to close"); | |
| Console.ReadLine(); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Web.Http; | |
| using Owin; | |
| namespace RestTest | |
| { | |
| public class Startup | |
| { | |
| public void Configuration(IAppBuilder appBuilder) | |
| { | |
| var config = new HttpConfiguration(); | |
| config.Routes.MapHttpRoute("DefaultApi","api/{controller}/{id}",new { id = RouteParameter.Optional }); | |
| appBuilder.UseWebApi(config); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Linq; | |
| using System.Net; | |
| using System.Net.Http; | |
| using System.Reflection; | |
| using System.Web.Http; | |
| using log4net; | |
| namespace RestTest | |
| { | |
| public class TestController : ApiController | |
| { | |
| private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | |
| public HttpResponseMessage Post() | |
| { | |
| var route = Request.GetRouteData().Route.RouteTemplate; | |
| var method = Request.Method.Method; | |
| var url = Request.RequestUri.AbsoluteUri; | |
| Log.InfoFormat("{0} {1}, route: {2}, controller:{3}, action:{4}", method, url, route, GetType().Name, MethodBase.GetCurrentMethod().Name); | |
| Log.InfoFormat(string.Join("|", Request.Headers.Select(kvp => string.Format("Key:{0}, Value:{1}", kvp.Key, kvp.Value)))); | |
| Log.Info(Request.Content.ReadAsStringAsync().Result); | |
| return new HttpResponseMessage(HttpStatusCode.OK); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment