2

I want to configure log4net from xml file. I placed this file in root folder of the service. Property Copy to Output Directory is set to Copy Always and works fine — the file is getting copied.

Problem starts when I try to load this file with log4net.Config.XmlConfigurator.ConfigureAndWatch — it is not in the working directory, but in another directory with code.

I don't want to place configuration for log4net into app.config. It just doesn't work right now and I will spend lot of time on figuring this out.

Is there a way to add to a particular service some metadata(in files) and then access them?

Is there a way to do it in general and place files not in the root directory? For example, if I would want to have access to some binary data that cannot be placed in Settings.xml, how could I achieve this?

UPDATE

Looks like the only thing that needs to be done to copy files is to set their property to Copy Always. So, my real issues is that I have C:\SfDevCluster\[..]\work as working directory and not the directory with actual binaries.

cassandrad
  • 3,412
  • 26
  • 50

2 Answers2

1

You can use a custom configuration package or data package for this. Access the files using the ServiceContext object.

More info here

LoekD
  • 11,402
  • 17
  • 27
  • Vaclav Turecek [wrote](http://stackoverflow.com/questions/36786078/where-how-does-one-instantiate-serviceinitializationparameters-in-an-azure-ser) that in the latest on the moment version of SF `ServiceInitializationParameters` is replaced with `Context` of a service. That means that I can't use it to instantiate logger in host process before I created a service. That's why this is not a solution for me. Or am I missing something? – cassandrad Jul 13 '16 at 07:56
  • The context is a constructor parameter of the Service, if you want you can use it to initialize the logger in Program.Main, in the Func that creates the Service instance. – LoekD Jul 13 '16 at 08:42
  • okay, I got it. I need to get `Settings.xml`, then get its path, then add the name of my custom config file. Thanks, it works. Still, if I want to log something before I instantiated a service, I don't have possibility to get any configs as there is no access to `Context`. – cassandrad Jul 13 '16 at 09:06
0

You could also use AppDomain.CurrentDomain.BaseDirectory to get the directory where the binaries are located, which is also the same directory where the log4net.config file should be.

This won't help you if you're using .net core, since it no longer has an app domain, but then you could use something like Assembly.GetEntryAssembly() and get it's path from there. You'd need to look up the exact syntax.

rakker91
  • 1
  • 1