Consists of three assemblies / untilities that may one day be split into seperate packages.
This library is a set of custom implementations for the existing 'System.Diagnostics.TraceListener'. Currently included is a RabbitMq listener which sends traces to a message queue for consumption.
To begin using:
-
Use NuGet to
Install-Package Recodify.Logging.Listeners
-
Add the following keys to web.config appSettings.
-
Add the below configuration to
Web.config
<system.diagnostics> <trace autoflush="true" /> <sources> <source name="Fallback" switchValue="Information, Error, Warning"> <listeners> <!-- This is optional and will log any errors publishing to RabbitMQ and can be useful for diagnosing initial setup --> <add name="FallBackTraceListners" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" /> </listeners> </source> <source name="YourSourceName" switchValue="Information, Error, Warning"> <listeners> <add name="YourListenerName" type="Recodify.Logging.Listeners.RabbitMq.TraceListener, Recodify.Logging.Listeners.RabbitMq" initializeData="exchangeName,queueName,componentName " /> </listeners> </source> </sources> </system.diagnostics>
-
You are now ready to start creating some logs.
If you are using an IOC container, registered the jobLogger with your traceSourceName:
container.RegisterInstance(typeof(IJobLogger), new JobLogger("BillingDetails"));
Then simply inject IJobLogger as a dependency e.g:
public MyClass (IJobLogger jobLogger) { jobLogger.TraceData(TraceEventType.Information, (int)Event.MyEvent, "Your Message"); }
If you are not using an IOC container, just new up a logger:
var jobLogger = new JobLogger("BillingDetails"); jobLogger.TraceData(TraceEventType.Information, (int)Event.MyEvent, "Your Message");
-
If you are operating in a web environment. You can stamp all logs with the request Id by adding the following to your `Global.asax
protected void Application_BeginRequest(object sender, EventArgs e) { if (HttpContext.Current != null) { HttpContext.Current.Items.Add("RequestId", Guid.NewGuid()); } }
- Castle.Core version="3.2.2"
- Castle.Windsor" version="3.2.1"
- EasyNetQ" version="0.34.0.279"
- EasyNetQ.Management.Client" version="0.39.2.333"
- Iesi.Collections" version="3.2.0.4000"
- Newtonsoft.Json" version="7.0.1"
- RabbitMQ.Client" version="3.3.2"
This library wraps an existing System.Diagnostics.TraceSource
with additional sanitisation functionality.
This alleviates the risk of logging sensitive data such as passwords and ending up in unsafe hands.
To begin using:
- Use NuGet to
Install-Package Recodify.Logging.Trace
- Wherever an existing
TraceSource
is required you can useITraceSource
and inject the concrete implementationSanitisedTraceSource
or generalTraceSource
None
This library can be used to add logging to existing Web API 2.2 projects.
Included is LogHandler
, an implementation of a DelegatingHandler
that provides trace logging for Web API requests/responses.
An implementation of an ITraceSource
(based on System.Diagnostics.TraceSource
) is required for both request and response.
To begin using:
Install-Package Recodify.Logging.WebApi
- Within your
WebApiConfig.cs
do the following (You can also use the generalTraceSouce
if data sanitisation is not required) - See below:
.
public static partial class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var requestTraceSource = new SanitisedTraceSource("Request", new Sanitiser());
var responseTraceSource = new SanitisedTraceSource("Response", new Sanitiser());
config.MessageHandlers.Add(new LogHandler(requestTraceSource, responseTraceSource, new HttpContext(), new Options()));
}
}
WebAPI logging is provided using an async delegating message handler that makes use of the await syntax. The await syntax is not supported in asp.net 4.0 so you must use asp.net 4.5. If you are upgrading a 4.0 project to 4.5, you will need to ensure that you have the following in your web.config:
<system.web>
<httpRuntime targetFramework="4.5" />
see: https://blogs.msdn.microsoft.com/webdev/2012/11/19/all-about-httpruntime-targetframework/
- Newtonsoft.Json version="7.0.1"
- Recodify.Logging.Trace