Sometimes to might want to write to a separate file, e.g. when importing stuff. Is there a way to use LogHelper to write to a different file in some parts of the code or have a similar class which wrap to custom logger?
At the moment I have the following in /Config/log4net.config
You can handle this by only modifying the log4net configuration a bit, and then you can keep on using the static LogHelper class like you're used to.
I've used this in the past, but I think something has changed a bit since then - so this may be a bit outdated. But from what I can see, it still works.
When you set up a new Umbraco installation, your log4net.config file will have an appender element, that looks like this:
It's basically the configuration for the default logger. We can take a copy of that element to creator our own, but instead for calling it rollingFile, we could call it OurHestAppender like below:
Also notice that I've changed the type attribute to Umbraco.Core.Logging.AsynchronousRollingFileAppender, Umbraco.Core. I think this is necessary to get the namespace-based logic to work.
You can then add a new logger element, that then uses the appender above:
With this in place, whenever you log something for a class within the Our.hest namespace, that information is logged to your custom log file ;)
It appears that the same information is also appended to the main Umbraco log file. I don't think it used to be like that in the past, but I cant remember for sure.
Anyways, with the changed list above, my entire log4net.config file then looks like:
Thanks for the suggestions.
Does that mean when using LogHelper only code inside the namespace Our.Hest is appended to the custom log file - would code inside the namespace Our.Hest.Unicorn also be appended to this log file or does it have the be the specific namespace?
In my case I have MyProject.Library.Import where I want to use the custom log file. Futhermore I would also prefer it doesn't write to the Umbraco log file too as it doesn't in the code I posted.
I just want to share with you my approach on this matter as I believe that could be a great way for many projects or custom umbraco implementations.
The scenario is with multiple projects within your solution file. Let's say you have:
MyProj.Frontend.Web (with umbraco cms install)
MyProj.Frontend.Core (with some of our own custom code)
MyProj.Core.Data (with some of our own data access layer code)
Now you would like to have each namespace/dll with is own log file with the corresponding log instructions on each file according to with the code context execution.
It's start with the changes on the log4net.config file as mention before. You should have a declaration of 3 + 1 (of umbraco.core) logger attributes for each one above:
declare the container for the appender that you defined on the previous step.
When you run your solution, you'll end up with:
Umbraco.Core.hostname.log
MyProj.Frontend.Core.hostname.log
MyProj.Core.Data.hostname.log
I hope that it helps because in my opinion is very useful that we could have different log files for each layer of our custom application that uses the umbraco cms. As of this matter, it allows you to focus or attention on the correct layer when you're facing issues.
Write log to custom separate file
When using the static
LogHelper
class in Umbraco it writes by default to UmbracoTraceLog file. https://our.umbraco.org/apidocs/csharp/api/Umbraco.Core.Logging.LogHelper.htmlSometimes to might want to write to a separate file, e.g. when importing stuff. Is there a way to use
LogHelper
to write to a different file in some parts of the code or have a similar class which wrap to custom logger?At the moment I have the following in
/Config/log4net.config
Then in my import class I have.
Can I somehow wrap the logger in a class similar to
LogHelper
so I don't need to callGetLogger()
in the different files where I want to log details?/Bjarne
You can handle this by only modifying the log4net configuration a bit, and then you can keep on using the static LogHelper class like you're used to.
I've used this in the past, but I think something has changed a bit since then - so this may be a bit outdated. But from what I can see, it still works.
When you set up a new Umbraco installation, your
log4net.config
file will have anappender
element, that looks like this:It's basically the configuration for the default logger. We can take a copy of that element to creator our own, but instead for calling it
rollingFile
, we could call itOurHestAppender
like below:Also notice that I've changed the
type
attribute toUmbraco.Core.Logging.AsynchronousRollingFileAppender, Umbraco.Core
. I think this is necessary to get the namespace-based logic to work.You can then add a new
logger
element, that then uses the appender above:With this in place, whenever you log something for a class within the
Our.hest
namespace, that information is logged to your custom log file ;)It appears that the same information is also appended to the main Umbraco log file. I don't think it used to be like that in the past, but I cant remember for sure.
Anyways, with the changed list above, my entire
log4net.config
file then looks like:Hi Anders
Thanks for the suggestions. Does that mean when using
LogHelper
only code inside the namespaceOur.Hest
is appended to the custom log file - would code inside the namespaceOur.Hest.Unicorn
also be appended to this log file or does it have the be the specific namespace?In my case I have
MyProject.Library.Import
where I want to use the custom log file. Futhermore I would also prefer it doesn't write to the Umbraco log file too as it doesn't in the code I posted.Hi all,
I just want to share with you my approach on this matter as I believe that could be a great way for many projects or custom umbraco implementations.
The scenario is with multiple projects within your solution file. Let's say you have:
Now you would like to have each namespace/dll with is own log file with the corresponding log instructions on each file according to with the code context execution.
It's start with the changes on the log4net.config file as mention before. You should have a declaration of 3 + 1 (of umbraco.core) logger attributes for each one above:
Then the declaration of the appenders like:
and last, declarations for the appender log container itself (file, smtp, etc). For namespace Umbraco.Core:
and for your own:
Keep in mind that you would need:
When you run your solution, you'll end up with:
I hope that it helps because in my opinion is very useful that we could have different log files for each layer of our custom application that uses the umbraco cms. As of this matter, it allows you to focus or attention on the correct layer when you're facing issues.
Hi Bjarne.
Did you ever found a solution for this or did you go with Gonçalo solution?
is working on a reply...