Update to Nlog 5.0: Configuration not applied - c#

While upgrading from NLog 4.7.15 to 5.0.1 I found this test in our code base:
[Test]
public void CustomLogFactoryShouldBehaveCompletelyIndependent()
{
var memoryTarget4 = new MemoryTarget();
memoryTarget4.Layout = "${level}|${logger}|${message}${exception}";
var memoryTarget5 = new MemoryTarget();
var customConfig = new LoggingConfiguration();
customConfig.AddTarget("UnitTestLogger4", memoryTarget4);
customConfig.LoggingRules.Add(new LoggingRule("UnitTestLogger4", LogLevel.Trace, memoryTarget4));
customConfig.AddTarget("UnitTestLogger2", memoryTarget5);
customConfig.LoggingRules.Add(new LoggingRule("UnitTestLogger2", LogLevel.Info, memoryTarget5));
using (var customFactory = new LogFactory(customConfig)) // <<-- This ctor is marked obsolete
{
// Log logger defined only in custom config
var logger4 = customFactory.GetLogger("UnitTestLogger4");
logger4.Trace("Test4");
Assert.That(memoryTarget4.Logs.Count, Is.EqualTo(1));
Assert.That(memoryTarget5.Logs, Is.Empty);
memoryTarget4.Logs.Clear();
//... More cases tested here
}
}
The test works fine both in the old and the new version, but only after I disable the deprecation warning CS0618 for the constructor LogFactory(LoggingConfiguration).
To work around this, I tried to use the suggested alternative LoggingConfiguration(LogFactory), which connects the factory and the configuration basically the other way round.
[Test]
public void CustomLogFactoryShouldBehaveCompletelyIndependent()
{
var memoryTarget4 = new MemoryTarget();
memoryTarget4.Layout = "${level}|${logger}|${message}${exception}";
var memoryTarget5 = new MemoryTarget();
using (var customFactory = new LogFactory())
{
var customConfig = new LoggingConfiguration(customFactory);
customConfig.AddTarget("UnitTestLogger4", memoryTarget4);
customConfig.LoggingRules.Add(new LoggingRule("UnitTestLogger4", LogLevel.Trace, memoryTarget4));
customConfig.AddTarget("UnitTestLogger2", memoryTarget5);
customConfig.LoggingRules.Add(new LoggingRule("UnitTestLogger2", LogLevel.Info, memoryTarget5));
// customFactory.ReconfigExistingLoggers(); // <<-- Adding this changes nothing
// Log logger defined only in custom config
var logger4 = customFactory.GetLogger("UnitTestLogger4");
logger4.Trace("Test4");
Assert.That(memoryTarget4.Logs.Count, Is.EqualTo(1)); // <<-- Fails here, nothing was added to memoryTarget4.
Assert.That(memoryTarget5.Logs, Is.Empty);
memoryTarget4.Logs.Clear();
}
}
At least that's what I think should be changed. But the test fails now. The configuration is not applied, as the target does not get any logs.
What did I miss? What's the correct way of replacing the deprecated LogFactory(LoggingConfiguration) constructor here?

The constructor new LogFactory(customConfig) became obsolete to ensure that the LoggingConfiguration.Factory-option had the expected value (Using the intended isolated LogFactory instead of the global static LogManager.Factory)
You can replace:
customFactory.ReconfigExistingLoggers();
With this so the configuration is activated:
customFactory.Configuration = customConfig;
Alternative you could do this:
using var customFactory = new NLog.LogFactory().Setup().LoadConfiguration(builder => {
var memoryTarget4 = new MemoryTarget();
memoryTarget4.Layout = "${level}|${logger}|${message}${exception}";
var memoryTarget5 = new MemoryTarget();
builder.Configuration.LoggingRules.Add(new LoggingRule("UnitTestLogger4", LogLevel.Trace, memoryTarget4));
builder.Configuration.LoggingRules.Add(new LoggingRule("UnitTestLogger2", LogLevel.Info, memoryTarget5)));
}).LogFactory;
var logger4 = customFactory.GetLogger("UnitTestLogger4");
logger4.Trace("Test4");
Assert.That(memoryTarget4.Logs.Count, Is.EqualTo(1));
Assert.That(memoryTarget5.Logs, Is.Empty);
memoryTarget4.Logs.Clear();

Related

How do I direct log output to a file, using a FluentMigrator v3.2.15 In-Process runner?

I have a .NET 4.5.2 migration runner built using FluentMigrator 2.0.7 that "m trying to move to .NET 5.0 and FluentMigrator 3.2.15.
My current difficulty is writing the output to a text file.
var serviceProvider = new ServiceCollection()
.AddSingleton<ILoggerProvider, SqlScriptFluentMigratorLoggerProvider>()
.Configure<LogFileFluentMigratorLoggerOptions>(o => {
o.OutputFileName = "MyFilename.log";
})
.AddLogging(lb => lb.AddFluentMigratorConsole())
.Configure<FluentMigratorLoggerOptions>(o =>
{
o.ShowSql = true;
o.ShowElapsedTime = true;
})
.AddFluentMigratorCore()
.ConfigureRunner(builder =>
builder
.AddSqlServer2016()
.WithGlobalConnectionString(this.options.connectionString.ExpandDataDirectory())
.WithMigrationsIn(this.assembly)
)
.BuildServiceProvider();
using (var scope = serviceProvider.CreateScope())
{
var runner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>();
if (this.options.reverseMigration)
runner.MigrateDown(0);
else
runner.MigrateUp();
}
My problem is simple - when I try to run the migration I get an error:
Unable to resolve service for type 'System.IO.TextWriter' while attempting to activate 'FluentMigrator.Runner.Logging.SqlScriptFluentMigratorLoggerProvider'.
What's going on is simple enough - TextWriter is an abstract class, it can't be initiated.
But how do I configured the ServiceProvider so that when it's asked for a TextWriter, it returns a StreamWriter writing to the OutputFileName I provided to LogFileFluentMigratorLoggerOptions?
===
Edited:
I can do this:
using var logStream = new FileStream(this.options.outputFilename, FileMode.Append);
using var sw = new StreamWriter(logStream);
var serviceProvider = new ServiceCollection()
.AddSingleton<TextWriter>(sw)
.AddSingleton<ILoggerProvider, SqlScriptFluentMigratorLoggerProvider>()
...
But it strikes me as being very ugly...
I could write an sql output to a text file by registering LogFileFluentMigratorLoggerProvider as a singleton and then configuring it. LogFileFluentMigratorLoggerProvider inherits from SqlScriptFluentMigratorLoggerProvider and takes care to instantiate a StreamWriter for you.
private static IServiceProvider CreateServices()
{
// var connectionString = ""; grab your connection string
return new ServiceCollection()
.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
.AddSqlServer2016()
.WithGlobalConnectionString(connectionString)
.WithMigrationsIn(this.assembly))
.AddLogging(lb => lb.AddFluentMigratorConsole())
.AddSingleton<ILoggerProvider, LogFileFluentMigratorLoggerProvider>()
.Configure<LogFileFluentMigratorLoggerOptions>(
opt =>
{
opt.OutputFileName = "C:\\TEMP\\DatabaseMigration.sql";
opt.OutputGoBetweenStatements = true;
opt.ShowSql = true;
})
.BuildServiceProvider(false);
}

Simple Injector 4.1: Overriding Parameter Injection Behavior

I'm new to using DI in C# and had a look at Windsor, Ninject, Autofac, Unity and Simple Injector. I originally discarded Simple Injector because I needed value-type injection (fx. connection strings) but found a blogpost describing this: https://cuttingedge.it/blogs/steven/pivot/entry.php?id=94. Unfortunately the blogpost is outdated since IDependencyInjectionBehavior.BuildExpression is deprecated in version 4.1 and IDependencyInjectionBehavior.GetInstanceProducer has been introduced instead.
I'm not sure how to do what the blogpost describes with the new InstanceProducer. InstanceProducer has a static method FromExpression but I'm not sure which type etc. should be used.
I currently do Pure DI and have the following settings:
// Settings
var conLocal = ConfigurationManager.ConnectionStrings["APIPortMan"].ConnectionString;
var con = ConfigurationManager.ConnectionStrings["PortMan"].ConnectionString;
var conAzure = ConfigurationManager.ConnectionStrings["Azure"].ConnectionString;
var conSitecore = ConfigurationManager.ConnectionStrings["Sitecore"].ConnectionString;
var azureStorageAccount = ConfigurationManager.AppSettings.Get("StorageConnection");
var reportUploadPath = ConfigurationManager.AppSettings.Get("ReportUploadPath");
var PfsmlPath = ConfigurationManager.AppSettings.Get("PfsmlPath");
var reloadCounter = int.Parse(ConfigurationManager.AppSettings.Get("transactionServiceReloadCounter"));
var systemStartDate = DateTime.Parse(ConfigurationManager.AppSettings.Get("holdingServiceStartDate"));
var semaphoreCount = int.Parse(ConfigurationManager.AppSettings.Get("semaphoreCount"));
Then I have some repositories consuming these settings:
// Repositories
var _accountRepository = new AccountRepository(con, conAzure);
var _aggregatedPortfolioRelationshipRepository = new AggregatedPortfolioRelationshipRepository(conAzure);
var _aggregatedClientRelationshipRepository = new AggregatedClientRelationshipRepository(con, conAzure);
var _assetBondRepository = new AssetBondRepository(con, conAzure);
var _assetClassRepository = new AssetClassRepository(con, conAzure);
var _assetDerivativeRepository = new AssetDerivativeRepository(con, conAzure);
var _assetRepository = new AssetRepository(con, conAzure);
var _benchmarkRepository = new BenchmarkRepository(con, conAzure);
var _benchmarkWeightRepository = new BenchmarkWeightRepository(con, conAzure);
var _clientRepository = new ClientRepository(con, conAzure);
var _defaultPriceRepository = new DefaultPriceRepository(con, conAzure);
var _emailRepository = new UpdateEmailOutput(conAzure);
var _exchangeRateRepository = new ExchangeRateRepository(con, conAzure);
var _failedHoldingRepository = new FailedHoldingRepository(conLocal);
var _GICSRepository = new GICSRepository(con, conAzure);
var _holdingRepository = new HoldingRepository(conAzure);
var _limitLineRepository = new LimitLineRepository(con, conAzure);
var _PFSMLRepository = new PFSMLRepository(PfsmlPath);
var _portfolioRepository = new PortfolioRepository(con, conAzure);
var _sitecoreReportRepository = new SitecoreReportRepository(conSitecore, reportUploadPath);
var _systemInfoRepository = new SystemInfoRepository(conAzure);
var _transactionRepository = new TransactionRepository(con, conAzure);
And later some services consuming the repositories and a few of the settings.
Since most of the repositories share a common interface IRepository<T> (besides an individual interface like IAssetRepository that extends IRepository with the type Asset) I would like to be able to use batch creation /auto-wiring. Also I would like to avoid changing the DI setup every time a change is made to my main code (ie. new constructor parameter, new repository interface/class etc.). Also I would like to avoid using lambda's, "new" and getInstance in the registration process because this will require changes to the DI setup whenever I change a constructor.
I have already adopted the convention mentioned in the blogpost (using AzureConnectionString, PortManConnectionString etc.) in the constructors. All I need to do now is make sure Simple Injector handles value type parameters according to the conventions :)
I posted the same question on Github (as Steven noted).
https://github.com/simpleinjector/SimpleInjector/blob/v4.0.x/src/SimpleInjector.CodeSamples/ParameterConventionExtensions.cs contains an updated version of the convention-based approach although Steven/dotnetjunkie convinced me to take a different approach (using settings-objects).

How to set ImageResizer configuration programmatically?

I want to set the configuration parameter clientcache.minutesprogrammatically but im struggling with the config design in ImageResizer.
My approach currently is:
var lWebConfigReader = new System.Xml.XmlTextReader(#"Web.config");
var lXmlDocument = new System.Xml.XmlDocument();
lXmlDocument.Load(lWebConfigReader);
var lResizerNode = lXmlDocument.SelectSingleNode("/configuration/resizer");
var lSection = new ImageResizer.ResizerSection(lResizerNode.OuterXml);
var lConfig = new ImageResizer.Configuration.Config(lSection);
int mins = lConfig.get("clientcache.minutes", -1);
...
ImageResizer.Configuration.Config.Current.setConfigXml(lConfig.getConfigXml());
It seems a bit hacky and also doesn't work as the ClientCache plugin doesn't sent the Expires header as it normally should when clientcache.minutes is set.
What could be the issue?
After some digging in the source code i found out that in this particular case you need to alter the global configuration object as the ClientCache plugin reads the parameter via Get() from it. So my current solution is:
// read a XML where a <resizer>...</resizer> is present, in this case a typical Web.config as mentioned in the ImageResizer docs
var lWebConfigReader = new System.Xml.XmlTextReader(#"Web.config");
var lXmlDocument = new System.Xml.XmlDocument();
lXmlDocument.Load(lWebConfigReader);
// read the resizer tag to a node
var lResizerNode = lXmlDocument.SelectSingleNode("/configuration/resizer");
// create a section from the node
var lSection = new ImageResizer.ResizerSection(lResizerNode.OuterXml);
// create a new config object from the section
var lConfig = new ImageResizer.Configuration.Config(lSection);
// override the global configugration with the newly created one
ImageResizer.Configuration.Config.Current.setConfigXml(lConfig.getConfigXml());
// test the Get() call used by the ClientCache plugin
int mins = ImageResizer.Configuration.Config.Current.get("clientcache.minutes", -1);
This code could be placed in a ICurrentConfigProvider implementation or Application_Start() in Global.asax.

Azure Continuous Deployment - Code First Migration seeding issue (MVC 5)

I've set up the Continuous Deployment in Microsoft Azure (Web App) using a ButBucket Git repo. Code First Migrations works well on my computer, it creates tables and seeds them, but when I sync the branch, the seed method of the migration is not run on Azure.
So Azure gets the changes from BitBucket, creates the tables as needed, but does not run the seed method (every table remains empty).
Can you suggest a solution to run the Seed method on Azure automatically when a new migration is applied (or after every time Azure builds from BitBucket if that is the only solution)?
Additional Info:
MigrationHistory table contains the migrations, so they were run.
I've set AutomaticMigrationsEnabled = true; but the problem remains
On Azure there is a Web App which is built and migrated, and an SQL Database which is referenced in the ConnectionString in Web.config
Configuration.cs
internal sealed class Configuration : DbMigrationsConfiguration<MyInsidR.Models.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
ContextKey = "MyInsidR.Models.ApplicationDbContext";
}
protected override void Seed(ApplicationDbContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
context.Prophecies.AddOrUpdate(p => p.ID,
new Prophecy() { ID = 1, Text = "Fűben iszogatós, sírva nevetős."}
);
context.Interesteds.AddOrUpdate(x => x.ID,
new Interested() { ID = 1, Email = "teszt.elek#gmail.com", FirstName = "Elek", LastName = "Teszt", RegistrationDate = DateTime.Now }
);
var tag1 = new Tag() { ID = 1, Name = "Karaoke", ApplyTo = TagApplication.All, Type = TagType.Games };
var tag3 = new Tag() { ID = 3, Name = "4 rooms", ApplyTo = TagApplication.All, Type = TagType.Misc };
var tag4 = new Tag() { ID = 4, Name = "Helipad", ApplyTo = TagApplication.All, Type = TagType.Vip };
context.Tags.AddOrUpdate(x => x.ID,
tag1, tag3, tag4
);
var indicatorIcon1 = new IndicatorIcon() { ID = 1, VisualClass = IndicatorIcon.VisualClassType.Hidden, Name = "No Indicator Icon", Description = "Nothing special, just a regular place or event." };
var indicatorIcon2 = new IndicatorIcon() { ID = 2, VisualClass = IndicatorIcon.VisualClassType.Fire, Name = "Hot", Description = "This place or event is very popular at the moment. There are big parties and a big fuss around it." };
context.IndicatorIcons.AddOrUpdate(x => x.ID,
indicatorIcon1, indicatorIcon2
);
AddUserAndRole(context);
}
bool AddUserAndRole(ApplicationDbContext context)
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var identityResult = roleManager.Create(new IdentityRole("Admin"));
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var user = new ApplicationUser()
{
UserName = "myinsidr#gmail.com",
};
identityResult = userManager.Create(user, "Qwertz1234!");
if (identityResult.Succeeded == false)
return identityResult.Succeeded;
identityResult = userManager.AddToRole(user.Id, "Admin");
return identityResult.Succeeded;
}
}
(I've found questions and solutions related to seed method issue only for direct deployment from Visual Studio, but that's not the way I would like to go.
Also there are solutions using different SQL management projects, but I think code first migrations inside the MVC project is the cleanest solution if it works like on my local machine)
I have found out how to run the Seed method every server start using this technique: http://romiller.com/2012/02/09/running-scripting-migrations-from-code/
Running Seed at every server start is pretty good for me, since it will run after every build by Azure Continuous Deployment. Of course it will run in other cases as well, but my method is not too long, so it does not matter.
I put the following code to Global.asax --> Application_Start():
var migrator = new DbMigrator(new Configuration());
migrator.Update();
As
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// CODE FIRST MIGRATIONS
#if !DEBUG
var migrator = new DbMigrator(new Configuration());
migrator.Update();
#endif
}
What this does is basically running a Code First Migration at every server start.

Azure cache programatic configuration results in ErrorCode ERRCA0029 SubStatus ES0001 Authorization token passed by user Invalid

I am trying Proof of Concepts based on code at http://msdn.microsoft.com/en-us/library/windowsazure/gg618003. This cache is accesible if I use app.config settings. When I switched the application to use programatic configuration, I consistently get this error. I have already tried Azure cache programatically configuration fail to verify and many other solutions to no avail.
Here's my code snippet.
{code}
String acsKey = "AcsKey removed intentionaly";
DataCacheFactoryConfiguration cacheFactoryConfiguration;
DataCacheSecurity dataCacheSecurity;
DataCacheServerEndpoint[] serverEndpoints = new DataCacheServerEndpoint[1];
SecureString secureAcsKey = new SecureString();
serverEndpoints[0] = new DataCacheServerEndpoint("EndPont removed intentionaly", 22243);
//
// Create SecureString from string
//
foreach (char keyChar in acsKey)
{
secureAcsKey.AppendChar(keyChar);
}
secureAcsKey.MakeReadOnly();
dataCacheSecurity = new DataCacheSecurity(secureAcsKey);
//
// Initialize Factory Configuration
//
cacheFactoryConfiguration = new DataCacheFactoryConfiguration(); // This line throws exception. Note that the key is yet to be assigned to SecurityProperties as per documentation.
cacheFactoryConfiguration.Servers = serverEndpoints;
cacheFactoryConfiguration.SecurityProperties = dataCacheSecurity;
_cacheFactory = new DataCacheFactory(cacheFactoryConfiguration);
_cache = _cacheFactory.GetDefaultCache();
{code}
Try passing all the params at creation and not post creation?
var configFactory = new DataCacheFactoryConfiguration
{
Servers =
new List<DataCacheServerEndpoint>
{new DataCacheServerEndpoint(cacheServer, cachePort)},
SecurityProperties =
new DataCacheSecurity(Encryption.CreateSecureString(cacheAuthorization),
true)
};

Categories