NLog how to use class member variable in log? - nlog

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}

Related

java.lang.ClassCastException: org.apache.log4j.Logger cannot be cast

I extended org.apache.log4j.Logger for implementing logging for method starts and exists.
It works fine, when I don't set the loglevel for a class in my log4j.properties.
When I set
log4j.logger.de.martinm.tools.UniCredit.ExportOperator=INFO
I get an exception:
Exception in thread "main" java.lang.ClassCastException: org.apache.log4j.Logger cannot be cast to de.martinm.tools.Logging.MMLogger
at de.martinm.tools.UniCredit.ExportOperator.(ExportOperator.java:21)
at de.martinm.tools.UniCredit.ExportOperator.main(ExportOperator.java:330)
public class MMLogger extends Logger {
private static MyLoggerFactory myFactory = new MyLoggerFactory();
public MMLogger(String name) {
super(name);
}
public static Category getInstance(String name) {
return Logger.getLogger(name, myFactory);
}
public static Logger getLogger(String name) {
return Logger.getLogger(name, myFactory);
}
public void enter(Logger logger, String method) {
super.debug(method+" enter");
}
public void exit(Logger logger, String method) {
super.debug(method+" exit");
}
public void debug(Logger logger, String method, String text) {
super.debug(method+" "+text);
}
public void warn(Logger logger, String method, String text) {
super.warn(method+" "+text);
}
public void info(Logger logger, String method, String text) {
super.info(method+" "+text);
}
public void error(Logger logger, String method, String text) {
super.error(method+" "+text);
}
}
public class MyLoggerFactory implements LoggerFactory {
/**
The constructor should be public as it will be called by
configurators in different packages. */
public
MyLoggerFactory() {
}
public
Logger makeNewLoggerInstance(String name) {
return new MMLogger(name);
}
}
Here is part of my code
public class ExportOperator {
//public static Logger logger = Logger.getLogger(ExportOperator.class.getName());
public MMLogger Mylogger = (MMLogger) MMLogger.getLogger(ExportOperator.class.getName());
public Connection db_con;
static Utils my_utils = new Utils();
public Properties props = new Properties();
public String output_dir;
public int mid;
public String admin_id;
public int op_id;

Generic Automapper function with custom convension for underscored properties

I simply need to map some auto generated classes from database to domain/viewmodels classes. The autogenerated class may have names like customer_id that I want to be mapped with CustomerId. Somehow I want to register my own convention with auto mapper. I have started with following code however the mapped object properties are not populated:
// Generic method that should map source to target
public static TTarget MapWithUnderScoreConvension(TSource source, TTarget target)
{
Mapper.Initialize(cfg=> cfg.AddProfile<AutoMapperUnderScoreProfile>());
Mapper.CreateMap<TSource, TTarget>();
var mapped = Mapper.Map(source, target);
return mapped;
}
// New underscore profile
public class AutoMapperUnderScoreProfile : Profile
{
public AutoMapperUnderScoreProfile()
{
Mapper.Initialize(configuration => configuration.CreateProfile("UnderScoreProfile", UnderScoreProfile));
Mapper.AssertConfigurationIsValid();
}
private void UnderScoreProfile(IProfileExpression profile)
{
profile.SourceMemberNamingConvention = new PascalCaseNamingConvention();
profile.DestinationMemberNamingConvention = new SourceUnderScoreNamingConvension();
}
}
// Convention class
public class SourceUnderScoreNamingConvension : INamingConvention
{
private readonly string _separatorCharacter="_";
private readonly Regex _splittingExpression = new Regex(#"[\p{Lu}0-9]+(?=_?)");
public Regex SplittingExpression { get { return _splittingExpression;} private set{} }
public string SeparatorCharacter { get { return _separatorCharacter; } private set{} }
}
// Test cases
[TestMethod()]
public void Test_Map_Db_Generated_Class_To_Model()
{
var dbGenerated = new QuestionTypeFromDb()
{
QuestionType_Description = "1",
QuestionType_Id = 1,
QuestionType_Is_Default = true,
QuestionType_Is_TimeBased = true,
QuestionType_Sequence = 1,
QuestionType_Time_In_Seconds = 1
};
var mappedObject = AutoMapperHelper<QuestionTypeFromDb, QuestionType>
.MapWithUnderScoreConvension(dbGenerated, new QuestionType());
mappedObject.QuestionTypeId.Should().Be(dbGenerated.QuestionType_Id);
mappedObject.QuestionTypeDescription.Should().Be(dbGenerated.QuestionType_Description);
mappedObject.TimeInSeconds.Should().Be(dbGenerated.QuestionType_Time_In_Seconds);
mappedObject.QuestionTypeSequence.Should().Be(dbGenerated.QuestionType_Sequence);
}
public class TestQuestionWithAnswerType
{
public int QuestionTypeId { get; set; }
public string QuestionTypeDescription { get; set; }
public int QuestionTypeSequence { get; set; }
public bool QuestionTypeIsTimeBased { get; set; }
public int? QuestionTypeTimeInSeconds { get; set; }
public bool QuestionTypeIsDefault { get; set; }
}
any comments will be appreciated.
Update
I have found that the following workaround works:
I simply replaced used this -> to replace 'underscore' with nothing (Mapper.Initialize(c => c.ReplaceMemberName("_", ""));
public static TTarget MapWithUnderScoreConvension(TSource source, TTarget target)
{
Mapper.Initialize(c => c.ReplaceMemberName("_", ""));
//Mapper.Initialize(cfg => cfg.AddProfile<AutoMapperUnderScoreProfile>());
Mapper.CreateMap<TSource, TTarget>();
var mapped = Mapper.Map(source, target);
return mapped;
}
Your regex needs to be changed to : [\p{L}}0-9]+(?=_?)
This will take care of Customer_Id, CUSTOMER_ID, customer_id
The {L} unicode category includes Lu, Lt, Ll, Lm and Lo characters as mentioned here.
Answer is added in the Update section of the question. Basically the solution for me was very simple -> Mapper.Initialize(c => c.ReplaceMemberName("_", ""));

Property injection with Unity

i encoutered problem with unity, i want to use property injection, here is what i had in my code :
config of the container :
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<GTModelContainer, GTModelContainer>(new HttpContextLifetimeManager<GTModelContainer>())
.RegisterType<IUnitOfWork, UnitOfWorkGT>()
.RegisterType<ILogger, Logger>(new ContainerControlledLifetimeManager())
.RegisterType<ISocieteServices, SocieteServices>() ;
}
SocieteService Class :
public class SocieteServices : ISocieteServices
{
private IUnitOfWork UnitOfWork;
public SocieteServices(IUnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
}
}
i tried to use property injection (i can't use constructor injection with custom data annotation) and here what i had done :
public class CodeSocieteUniqueAttribute : ValidationAttribute
{
[Dependency]
public ISocieteServices SocieteService {get; set;}
[InjectionMethod]
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string codeSociete = value as string;
var societe = SocieteService.getSocieteByCode(codeSociete);
if (societe == null) return ValidationResult.Success;
else return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
}
the problem is that the societeService in CodeSocieteUniqueAttribute class is not injected.
Assuming that your class for registering types is publicly accessible and has a IUnityContainer object, ie:
public static class Resolver
{
public static IUnityContainer Container { get; set; }
public static void RegisterTypes(IUnityContainer container)
{
// type registrations here
container.RegisterType<GTModelContainer, GTModelContainer>(new HttpContextLifetimeManager<GTModelContainer>())
.RegisterType<IUnitOfWork, UnitOfWorkGT>()
.RegisterType<ILogger, Logger>(new ContainerControlledLifetimeManager())
.RegisterType<ISocieteServices, SocieteServices>() ;
// Now, set the container
Container = container;
}
}
You could access the container you've built up and resolve these types during method execution.
For instance,
public class CodeSocieteUniqueAttribute : ValidationAttribute
{
[Dependency]
public ISocieteServices SocieteService { get; set; }
[InjectionMethod]
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var societeServices = Resolver.Container.Resolve<ISocieteServices>();
SocieteService = societeServices; // Or, you know, just use this since it's resolved.
string codeSociete = value as string;
var societe = SocieteService.getSocieteByCode(codeSociete);
if (societe == null) return ValidationResult.Success;
else return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
}
This is actually pretty standard practice, and this MSDN article describes resolving items during runtime.
Another option is to pop the resolution into a default constructor like this:
public class CodeSocieteUniqueAttribute : ValidationAttribute
{
[Dependency]
public ISocieteServices SocieteService {get; set;}
public CodeSocieteUniqueAttribute()
{
var societeServices = Resolver.Container.Resolve<ISocieteServices>();
SocieteService = societeServices;
}
// the rest of the class omitted for brevity
}

Contra variance with interfaces

I have an interface iExportColumn and a class ExportColumn implementing the interfce. ExportColumnCollection class should be generic for all the classes that implements IExportColumn interface.
public interface IExportColumn
{
string Header { get; set; }
string ColumnName { get; set; }
}
public class ExportColumn : IExportColumn
{
public ExportColumn(){}
public string Header { get; set; }
public string ColumnName { get; set; }
}
public class ExportColumnCollection<T> where T: IExportColumn
{
private List<T> cols;
public ExportColumnCollection (List<T> c)
{
cols = c;
}
public T Columninfo (string colname)
{
}
.....
}
I am getting run time error saying could not load type ExportColumnCollection.
I am looking for something to achieve somthing like
List<IExportColumn> = new List<ExportColumn>();
I have two classes implementing the interfaces IExportColumn and I need to hold a GenericCollection to handle List
The proper usage for your class would be:
List<IExportColumn> list= new List<IExportColumn>();
// you may add to the collection any class which implements IExportColumn
list.Add(new ExportColumn1() { ColumnName = "Id" });
list.Add(new ExportColumn2() { ColumnName = "Value" });
ExportColumnCollection<IExportColumn> collection = new
ExportColumnCollection<IExportColumn>(list);
var colInfo = collection.ColumnInfo("Id");

AutoMapper mapping properties with private setters

Is it possible to assign properties with private setters using AutoMapper?
AutoMapper allows now (I am not sure, since when) to map properties with private setters. It is using reflection for creating objects.
Example classes:
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
}
public class PersonDto
{
public string Fullname { get; private set; }
}
And mapping:
AutoMapper.Mapper.CreateMap<Person, PersonDto>()
.ForMember(dest => dest.Fullname, conf => conf.MapFrom(src => src.Name + " " + src.Surname));
var p = new Person()
{
Name = "John",
Surname = "Doe"
};
var pDto = AutoMapper.Mapper.Map<PersonDto>(p);
AutoMapper will map property with private setter with no problem. If you want to force encapsulation, you need to use IgnoreAllPropertiesWithAnInaccessibleSetter. With this option, all private properties (and other inaccessible) will be ignored.
AutoMapper.Mapper.CreateMap<Person, PersonDto>()
.ForMember(dest => dest.Fullname, conf => conf.MapFrom(src => src.Name + " " + src.Surname))
.IgnoreAllPropertiesWithAnInaccessibleSetter();
The problem will emerge, if you will use Silverlight. According to MSDN: https://msdn.microsoft.com/en-us/library/stfy7tfc(v=VS.95).aspx
In Silverlight, you cannot use reflection to access private types and members.
If you set the value for this properties in the constructor like this
public class RestrictedName
{
public RestrictedName(string name)
{
Name = name;
}
public string Name { get; private set; }
}
public class OpenName
{
public string Name { get; set; }
}
then you can use ConstructUsing like this
Mapper.CreateMap<OpenName, RestrictedName>()
.ConstructUsing(s => new RestrictedName(s.Name));
which works with this code
var openName = new OpenName {Name = "a"};
var restrictedName = Mapper.Map<OpenName, RestrictedName>(openName);
Assert.AreEqual(openName.Name, restrictedName.Name);
See #600 Private/internal destination properties.
Solution:
public class PrivateInternalProfile {
protected override Configure() {
ShouldMapField = fieldInfo => true;
ShouldMapProperty = propertyInfo => true;
CreateMap<User, UserDto>(); //etc
}
}

Resources