Is there a way to set a context property value in log4net at logger level? We have scopes at thread context and global context and so on. I was wondering if there is a way to set a context variable at the logger instance level?
I know such thing does not exist but to make my point, it would be like
private static ILog _Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
_Log.LoggerContext.Properties["myVar"] = "someValue";
//now every log with this logger will use somevalue for the myVar property.
Is there a way to do such thing?
As far as I know, that capability does not exist in log4net (or NLog for that matter). I do have an idea that should work. I don't know if it is a "good" idea or not, I will leave that for you to decide...
Briefly, you could write a custom PatternLayoutConverter (see this post for one example of how to do this). This converter will look for a "context" in your own static dictionary (similar to the static dictionary contexts that log4net already has). The "context" will be stored by logger name. The value in the dictionary will be another dictionary that will hold your variables.
This a little bit more involved than I am prepared to get into right now, but I will try to give some good pseudocode to show how it might work...
UPDATE:
I have added an implementation that works (at least in the minimal testing that I have done). I have defined a "context" to hold a property bag for each logger. I have also implemented a PatternLayoutConverter to retrieve the properties for a given logger.
(The code formatting does not seem to be honoring indentation).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Util;
using log4net.Layout.Pattern;
using log4net.Core;
namespace Log4NetTest
{
//
// Context container for loggers.
// Indexable by logger or logger name.
//
public interface IContext
{
IContextProperties this [ILog logger] { get; }
IContextProperties this [string name] { get; }
}
//
// Context properties for a specific logger.
//
public interface IContextProperties
{
object this [string key] { get; set; }
void Remove( string key );
void Clear( );
}
//
// Static class exposing the logger context container.
//
public static class LoggerProperties
{
private static readonly IContext context = new LoggerContext();
public static IContext Properties { get { return context; } }
}
internal class LoggerContext : IContext
{
private readonly IDictionary<string, IContextProperties> dict = new Dictionary<string, IContextProperties>();
#region IContext Members
//
// Get the properties asociated with this logger instance.
//
public IContextProperties this [ILog logger]
{
get
{
ILoggerWrapper w = logger as ILoggerWrapper;
ILogger i = w.Logger;
return this[i.Name];
}
}
//
// Get the properties associated with this logger name.
//
public IContextProperties this [string name]
{
get
{
lock (dict)
{
IContextProperties props;
if ( dict.TryGetValue( name, out props ) ) return props;
props = new LoggerContextProperties();
dict [name] = props;
return props;
}
}
}
#endregion
}
//
// Implementation of the logger instance properties.
//
internal class LoggerContextProperties : IContextProperties
{
private readonly IDictionary<string, object> loggerProperties = new Dictionary<string, object>();
#region IContextProperties Members
public object this [string key]
{
get
{
lock ( loggerProperties )
{
object value;
if ( loggerProperties.TryGetValue( key, out value ) ) return value;
return null;
}
}
set
{
lock ( loggerProperties )
{
loggerProperties [key] = value;
}
}
}
public void Remove( string key )
{
lock ( loggerProperties )
{
loggerProperties.Remove( key );
}
}
public void Clear( )
{
lock ( loggerProperties )
{
loggerProperties.Clear();
}
}
#endregion
}
public class LoggerContextPropertiesPatternConverter : PatternLayoutConverter
{
protected override void Convert( System.IO.TextWriter writer, LoggingEvent loggingEvent )
{
IContextProperties props = LoggerProperties.Properties[loggingEvent.LoggerName];
object value = props[Option];
if (value != null)
{
writer.Write(value);
}
else
{
writer.Write("{0}.{1} has no value", loggingEvent.LoggerName, Option);
}
}
}
}
Configure the appender to use the PatternLayoutConverter:
<appender name="debug" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %logger %-5p [LOGPROP = %LOGPROP{test}] %m%n"/>
<converter>
<name value="LOGPROP" />
<type value="Log4NetTest.LoggerContextPropertiesPatternConverter" />
</converter>
</layout>
</appender>
How to set the properties for a logger:
ILog loga = LogManager.GetLogger("A");
ILog logb = LogManager.GetLogger("B");
ILog logc = LogManager.GetLogger("C");
LoggerProperties.Properties[loga]["test"] = "abc";
LoggerProperties.Properties[logb]["test"] = "def";
LoggerProperties.Properties[logc]["test"] = "ghi";
loga.Debug("Hello from A");
logb.Debug("Hello from B");
logc.Debug("Hello from C");
The output:
A: 2011-07-19 10:17:07,932 [1] A DEBUG [LOGPROP = abc] Hello from A
B: 2011-07-19 10:17:07,963 [1] B DEBUG [LOGPROP = def] Hello from B
C: 2011-07-19 10:17:07,963 [1] C DEBUG [LOGPROP = ghi] Hello from C
Good luck!
Related
I have a custom AI Telemetry Initializer. Recently, I have started getting a compiler warning, saying Context.Properties is obsolete:
"TelemetryContext.Properties is obsolete. Use GlobalProperties to set global level properties. For properties at item level, use ISupportProperties.Properties.
1. How do I do that? Use ISupportProperties.Properties?
2. I am logging the tenant Id, and deviceID, from claims in the principal, per request. Is that considered app global or a support property?
public class JsonTelemetryInitializer : ITelemetryInitializer
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly SystemOptions _systemOptions;
/// <summary>
/// Constructor
/// </summary>
/// <param name="accessor">For accessing the http context</param>
/// <param name="systemOptions">System options</param>
public JsonTelemetryInitializer(IHttpContextAccessor accessor, IOptions<SystemOptions> systemOptions)
{
_httpContextAccessor = accessor;
_systemOptions = systemOptions.Value;
}
/// <summary>
/// Initialize the custom telemetry initializer
/// </summary>
/// <param name="telemetry">Telemetry</param>
public void Initialize(ITelemetry telemetry)
{
if (_httpContextAccessor.HttpContext == null)
{
return;
}
if (_httpContextAccessor.HttpContext.User.Identity.IsAuthenticated)
{
const string tenantId = "northstar_tenantid";
if (!telemetry.Context.Properties.ContainsKey(tenantId))
{
var user = _httpContextAccessor.HttpContext.User;
telemetry.Context.Properties[tenantId] =
ClaimsHelper.GetClaim<int>(user, TokenNames.TenantId).ToString();
var userId = ClaimsHelper.GetClaim<int>(user, TokenNames.UserId).ToString();
telemetry.Context.Properties["northstar_userid"] = userId;
var deviceId = ClaimsHelper.GetClaim<string>(user, TokenNames.DeviceId);
if (deviceId != null)
{
telemetry.Context.Properties["northstar_deviceid"] = deviceId;
}
telemetry.Context.User.Id = userId;
var sessionId = ClaimsHelper.GetClaim<string>(user, TokenNames.SessionId);
if (!string.IsNullOrEmpty(sessionId))
{
telemetry.Context.Session.Id = sessionId;
}
}
}
}
}
1.How do I do that? Use ISupportProperties.Properties?
Just cast the ITelemetry to ISupportProperties.
My example code as below:
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
public class MyTelemetryInitializer:ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
//cast ITelemetry to ISupportProperties
ISupportProperties propTelemetry = (ISupportProperties)telemetry;
if (!propTelemetry.Properties.ContainsKey("isuport_key"))
{
propTelemetry.Properties.Add("isuport_key", "isupport_value");
}
}
}
Then after execution, the property is shown in azure portal, screenshot as below:
2.I am logging the tenant Id, and deviceID, from claims in the principal, per request. Is that considered app global or a support property?
As per my understanding, you can use support property(item level) since it's similar with you did previously using like this telemetry.Context.Properties["northstar_deviceid"] = deviceId;
I had a similar problem and find this post.
They added documentation for adding properties to TelemetryContext.
Simply cast ITelemetry to RequestTelemetry.
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry == null) return;
requestTelemetry.Properties["customProp"] = "test";
}
https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#addmodify-properties-itelemetryinitializer
I don't see the need for a cast, you could do a type test
For example, in C# 7+:
public class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry is ISupportProperties telemetryWithProperties)
{
telemetryWithProperties.Properties["mykey"] = "test";
}
}
}
I am writing a program and I use NLog in it. I have several classes and in all of them I have the property
public string DeviceName { get; set; }
I want the logger to put this in front of my log message, so if for instance I have two classes
class A
{
public DeviceName {get;set;} = "HMP20"
public void methodOne()
{
logger.Info("Something");
}
}
class B
{
public DeviceName {get;set;} = "HMP30"
public void methodOne()
{
logger.Info("Something");
}
}
then the output in the log file should be
HMP20: Something
HMP30: Something
How does one achieve this?
The fast solution is to use the logger-name as device-name:
public class A
{
public string DeviceName {get => logger.Name; set => logger = NLog.LogManager.GetLogger(value); }
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
}
public class B
{
public string DeviceName {get => logger.Name; set => logger = NLog.LogManager.GetLogger(value); }
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
}
Then you can use the following layout-renderer: ${logger:shortname=true}
I am trying to learn IOC principle from this screencast
Inversion of Control from First Principles - Top Gear Style
I tried do as per screencast but i get an error while AutomaticFactory try create an object of AutoCue. AutoCue class has contructor which takes IClock and not SystemClock. But my question is , in screencast IClock is resolved with SystemClock while inside AutomaticFactory .But in my code , IClock does not get resolved . Am i missing something ?
class Program
{
static void Main(string[] args)
{
//var clarkson = new Clarkson(new AutoCue(new SystemClock()), new Megaphone());
//var clarkson = ClarksonFactory.SpawnOne();
var clarkson = (Clarkson)AutomaticFactory.GetOne(typeof(Clarkson));
clarkson.SaySomething();
Console.Read();
}
}
public class AutomaticFactory
{
public static object GetOne(Type type)
{
var constructor = type.GetConstructors().Single();
var parameters = constructor.GetParameters();
if (!parameters.Any()) return Activator.CreateInstance(type);
var args = new List<object>();
foreach(var parameter in parameters)
{
var arg = GetOne(parameter.ParameterType);
args.Add(arg);
}
var result = Activator.CreateInstance(type, args.ToArray());
return result;
}
}
public class Clarkson
{
private readonly AutoCue _autocue;
private readonly Megaphone _megaphone;
public Clarkson(AutoCue autocue,Megaphone megaphone)
{
_autocue = autocue;
_megaphone =megaphone;
}
public void SaySomething()
{
var message = _autocue.GetCue();
_megaphone.Shout(message);
}
}
public class Megaphone
{
public void Shout(string message)
{
Console.WriteLine(message);
}
}
public interface IClock
{
DateTime Now { get; }
}
public class SystemClock : IClock
{
public DateTime Now { get { return DateTime.Now; } }
}
public class AutoCue
{
private readonly IClock _clock;
public AutoCue(IClock clock)
{
_clock = clock;
}
public string GetCue()
{
DateTime now = _clock.Now;
if (now.DayOfWeek == DayOfWeek.Sunday)
{
return "Its a sunday!";
}
else
{
return "I have to work!";
}
}
}
What you basically implemented is a small IoC container that is able to auto-wire object graphs. But your implementation is only able to create object graphs of concrete objects. This makes your code violate the Dependency Inversion Principle.
What's missing from the implementation is some sort of Register method that tells your AutomaticFactory that when confronted with an abstraction, it should resolve the registered implementation. That could look as follows:
private static readonly Dictionary<Type, Type> registrations =
new Dictionary<Type, Type>();
public static void Register<TService, TImplementation>()
where TImplementation : class, TService
where TService : class
{
registrations.Add(typeof(TService), typeof(TImplementation));
}
No you will have to do an adjustment to the GetOne method as well. You can add the following code at the start of the GetOne method:
if (registrations.ContainsKey(type))
{
type = registrations[type];
}
That will ensure that if the supplied type is registered in the AutomaticFactory as TService, the mapped TImplementation will be used and the factory will continue using this implementation as the type to build up.
This does mean however that you now have to explicitly register the mapping between IClock and SystemClock (which is a quite natural thing to do if you're working with an IoC container). You must make this mapping before the first instance is resolved from the AutomaticFactory. So you should add the following line to to the beginning of the Main method:
AutomaticFactory.Register<IClock, SystemClock>();
I'm creating a self-hosted REST service using service stack & AppHostHttpListenerBase. I'd like to use a base URI for my services (e.g. "api") like so:
http://myserver/api/service1/param
http://myserver/api/service2/param
How do I do this without defining "api" in each of my routes. In IIS, I can set a virtual directory to isolate the services, but how do I do this when self-hosting?
Here ya go.. (as a bonus this is how you put your service into a plugin.
using BlogEngineService;
using ServiceStack.WebHost.Endpoints;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BlogEngineWinService
{
public class AppHost : AppHostHttpListenerBase
{
public AppHost() : base("Self Host Service", typeof(AppHost).Assembly) { }
public override void Configure(Funq.Container container)
{
Plugins.Add(new BlogEngine());
}
}
}
This is how you autowire it up
The call appHost.Routes.AddFromAssembly2(typeof(HelloService).Assembly); Is what calls the extension to auto wire.
using ServiceStack.WebHost.Endpoints;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.ServiceInterface;
namespace BlogEngineService
{
public class BlogEngine : IPlugin, IPreInitPlugin
{
public void Register(IAppHost appHost)
{
appHost.RegisterService<HelloService>();
appHost.Routes.AddFromAssembly2(typeof(HelloService).Assembly);
}
public void Configure(IAppHost appHost)
{
}
}
}
This is how you mark the Service Class to give it a prefix.
Simply mark the class with this attribute
using ServiceStack.DataAnnotations;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BlogEngineService
{
public class Hello
{
[PrimaryKey]
public string Bob { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
}
[PrefixedRoute("/test")]
public class HelloService : Service
{
public object Any(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Bob};
}
}
}
Create a CS file in your project for the extension..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ServiceStack.Common;
using ServiceStack.Common.Utils;
using ServiceStack.Common.Web;
using ServiceStack.Text;
using ServiceStack.ServiceHost;
using ServiceStack.WebHost.Endpoints;
using ServiceStack.ServiceInterface;
namespace ServiceStack.ServiceInterface
{
public static class ServiceRoutesExtensions
{
/// <summary>
/// Scans the supplied Assemblies to infer REST paths and HTTP verbs.
/// </summary>
///<param name="routes">The <see cref="IServiceRoutes"/> instance.</param>
///<param name="assembliesWithServices">
/// The assemblies with REST services.
/// </param>
/// <returns>The same <see cref="IServiceRoutes"/> instance;
/// never <see langword="null"/>.</returns>
public static IServiceRoutes AddFromAssembly2(this IServiceRoutes routes,
params Assembly[] assembliesWithServices)
{
foreach (Assembly assembly in assembliesWithServices)
{
AddNewApiRoutes(routes, assembly);
}
return routes;
}
private static void AddNewApiRoutes(IServiceRoutes routes, Assembly assembly)
{
var services = assembly.GetExportedTypes()
.Where(t => !t.IsAbstract
&& t.HasInterface(typeof(IService)));
foreach (Type service in services)
{
var allServiceActions = service.GetActions();
foreach (var requestDtoActions in allServiceActions.GroupBy(x => x.GetParameters()[0].ParameterType))
{
var requestType = requestDtoActions.Key;
var hasWildcard = requestDtoActions.Any(x => x.Name.EqualsIgnoreCase(ActionContext.AnyAction));
string allowedVerbs = null; //null == All Routes
if (!hasWildcard)
{
var allowedMethods = new List<string>();
foreach (var action in requestDtoActions)
{
allowedMethods.Add(action.Name.ToUpper());
}
if (allowedMethods.Count == 0) continue;
allowedVerbs = string.Join(" ", allowedMethods.ToArray());
}
if (service.HasAttribute<PrefixedRouteAttribute>())
{
string prefix = "";
PrefixedRouteAttribute a = (PrefixedRouteAttribute)Attribute.GetCustomAttribute(service, typeof(PrefixedRouteAttribute));
if (a.HasPrefix())
{
prefix = a.GetPrefix();
}
routes.AddRoute(requestType, allowedVerbs, prefix);
}
else
{
routes.AddRoute(requestType, allowedVerbs);
}
}
}
}
private static void AddRoute(this IServiceRoutes routes, Type requestType, string allowedVerbs, string prefix = "")
{
var newRoutes = new ServiceStack.ServiceHost.ServiceRoutes();
foreach (var strategy in EndpointHost.Config.RouteNamingConventions)
{
strategy(newRoutes, requestType, allowedVerbs);
}
foreach (var item in newRoutes.RestPaths)
{
string path = item.Path;
if (!string.IsNullOrWhiteSpace(prefix))
{
path = prefix + path;
}
routes.Add(requestType, restPath: path, verbs: allowedVerbs);
}
}
}
public class PrefixedRouteAttribute : Attribute
{
private string _prefix { get; set; }
private bool _hasPrefix { get; set; }
public PrefixedRouteAttribute(string path)
{
if (!string.IsNullOrWhiteSpace(path))
{
this._hasPrefix = true;
this._prefix = path;
//this.Path = string.Format("/{0}{1}", Prefix, Path);
}
}
public bool HasPrefix()
{
return this._hasPrefix;
}
public string GetPrefix()
{
return this._prefix;
}
}
}
ServiceStack's HttpListener hosts expects to be hosted a the root / path as the normal use-case is to have each self-hosted service available on different custom ports.
Since it doesn't currently support hosting at a /custompath, you would have to specify /api/ prefix on all your service routes.
Add an issue if you want to see support for hosting at custom paths.
There is actually an easier solution. In your web.config, update your http-handler to:
<httpHandlers>
<add path="api*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
With the above, all of your service apis must be prefixed with a "/api/". If you have already used "/api/" in any of your routes, you must now remove them or have to specify it twice in your calls.
Reference:
https://github.com/ServiceStack/SocialBootstrapApi
I've found a workaround for this. I've only tested this under self hosting.
Create a 'PrefixedRouteAttribute' class that inherits from RouteAttribute
public class PrefixedRouteAttribute : RouteAttribute
{
public static string Prefix { get; set; }
public PrefixedRouteAttribute(string path) :
base(path)
{
SetPrefix();
}
public PrefixedRouteAttribute(string path, string verbs)
: base(path, verbs)
{
SetPrefix();
}
private void SetPrefix()
{
if (!string.IsNullOrWhiteSpace(Prefix))
{
this.Path = string.Format("/{0}{1}", Prefix, Path);
}
}
}
When you create your AppHost you can set your Prefix
PrefixedRouteAttribute.Prefix = "api";
Then instead of using the [Route] attribute, use the [PrefixRoute] attribute on your classes
[PrefixedRoute("/echo")]
[PrefixedRoute("/echo/{Value*}")]
public class Echo
{
[DataMember]
public string Value { get; set; }
}
This will then work for requests to
/api/echo
/api/echo/1
This could possibly be improved. I don't really like the how I need to set the Prefix via the static property but I couldn't think of a better approach under my setup. The principle of creating the overriding attribute seems sound though, and that is the important part.
I have a base class in which I'm trying to use the Null Object pattern to provide a default logger implementation which can then be changed by IoC setter injection at a later stage.
public interface ILog
{
void Log(string message);
}
public class NoOpLogger: ILog
{
public void Log(string message)
{ }
}
public abstract class ClassWithLogger
{
private ILog _logger = new NoOpLogger();
protected ClassWithLogger()
{
Contract.Assert(Logger != null);
}
public ILog Logger
{
get { return _logger; }
set
{
Contract.Requires(value != null);
_logger = value;
Contract.Assert(Logger != null);
}
}
[ContractInvariantMethod]
private void ObjectInvariant()
{
Contract.Invariant(Logger != null);
}
}
public sealed class DerivedClass : ClassWithLogger
{
private readonly string _test;
public DerivedClass(string test)
{
Contract.Requires<ArgumentException>(!String.IsNullOrWhiteSpace(test));
_test = test;
// I get warning at end of ctor: "invariant unproven: Logger != null"
}
public void SomeMethod()
{
Logger.Log("blah");
}
}
As I indicate in the code, my issue is that I get a warning at the end of the derived class' constructor saying that the "Logger != null" object invariant from the base class has not been proven even though it's obvious nothing has changed the Logger property value and I also have contracts around the setter to ensure it can never be null anyway.
Is there any way to avoid having to reprove this fact in all derived classes, or is this just a limitation of the static analyser?
UPDATE: Problem has been fixed in latest version of CodeContracts. Also, I don't need the assert in the abstract base class constructor anymore (the line "Contract.Assert(Logger != null);")
I just tested your code as posted and it worked fine. Are you using the most recent version of Code Contracts (1.4.30707.2)?