Good morning, I am begginer using Umbraco, I would like knowing about logging in umbraco. I want to do custom logging for my site... what things should I consider for that? .. I hope any one can help me.. Thanks.
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.EventCollector
.WriteTo.File(.....)
.CreateLogger();
I want to use it in my controllers and I am doing this:
using Umbraco.Core.Logging;
public class UmbracoDefaultController : RenderMvcController
{
private readonly ILogger _logger;
public UmbracoDefaultController(ILogger logger)
{
_logger = logger;
}
public ActionResult Index(BasicViewModel viewModel)
{
var testname = "Hello first test - 01";
Logger.Info<UmbracoDefaultController>("Begin - {testname} ", testname);
}
}
Is it ok?...or Am I working in wrong way?..
Is there dependency injection?
if there is no , How I could work using dependency injection using injectlight from umbraco?
Did you get your example to work? When I use the docs like you have I just get "Logger does not contain a definition for Info". Which is true, it's "information" but then the syntax for the rest seems wrong.
I'm trying to use it in my own class in a V8.6.1 .core project (as opposed to .web).
Are you trying to create your own logging or use the Core Umbraco logging facilities?
If it is the latter and you are taking advantage of Dependency Injection, if you inject ILogger or IProfilingLogger (both are in the Umbraco.Core.Logging name space) then you can access them and log things as and when you need.
Both have .Info, .Warning, and .Error methods on them where you can pass the relevant information to the methods to be logged.
Thanks for your reply. I'm trying to use the Core Umbraco logging facilities. Although I've read and read about DI and understand what it's meant to do, I haven't yet managed to get my head around the syntax, it just doesn't say anything to me.
The class is "using Umbraco.Core.Logging;" but on Logger.Info<ImportProperties>("Error Saving Property ID: {PropertyId}", apiProperty.property_ID); I just get "Logger does not exist in the current context". Do I "new" something or what is the syntax to DI it? It's so confusing.
If I use the DI syntax from the Docs then I get "Logger does not contain a definition for Info".
I'm "just" updating a class called by an API from V7 to V8 ;)
The V7 code which worked was LogHelper.Error(MethodBase.GetCurrentMethod().DeclaringType, "Error Saving Property ID: " + property.Property_ID, ex);
Did you or anyone else find an approach to this issue?
Like Craig100, I too am struggling to wrap my mind around DI, especially in the context of moving from an ApplicationEventHandler in v7.4.2 to a Composer / Component in v8.6.2. Ultimately, my goal is to hook into Umbraco's messaging pipeline to display a custom message in the Backoffice when a user publishes a node that doesn't meet certain business rules.
I guess the question I'm left with is "how do I get a handle on whatever Logger Umbraco is currently using that implements ILogger?"
Here's a snippet of my DelegatingHandler, as is from the v7.4.2 implementation:
public class WebApiHandler : DelegatingHandler
{
private readonly ILogger _logger;
public WebApiHandler(ILogger logger)
{
_logger = logger;
}
}
Here's where I get stuck in my Composer in the v8.6.2 implementation:
public class WebApiComposer : ComponentComposer<WebApiComponent>, IUserComposer
{
public override void Compose(Composition composition)
{
System.Web.Http.GlobalConfiguration.Configuration.MessageHandlers.Add();
base.Compose(composition);
}
}
public class WebApiComponent : IComponent
{
public void Initialize()
{
// Method intentionally left empty.
}
public void Terminate()
{
// Method intentionally left empty.
}
}
What do I pass to .Add()? I know I can create an empty constructor and create a brand new instance of Serilog, but doesn't that defeat the purpose of DI?
Logging - Umbraco v8
Good morning, I am begginer using Umbraco, I would like knowing about logging in umbraco. I want to do custom logging for my site... what things should I consider for that? .. I hope any one can help me.. Thanks.
Hi Oscar
There is a good introduction here to serilog which Umbraco has baked in:
https://our.umbraco.com/Documentation/Getting-Started/Code/Debugging/Logging/
Particularly how you can write your own custom messages to the log.
Regards
Marc
Thanks Marc.
Good morning All:
I created a class and one method with this code :
using Serilog; using Serilog.Sinks.Splunk;
Log.Logger = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.EventCollector .WriteTo.File(.....) .CreateLogger();
I want to use it in my controllers and I am doing this:
Is it ok?...or Am I working in wrong way?.. Is there dependency injection? if there is no , How I could work using dependency injection using injectlight from umbraco?
I hope someone can help me.
Hi Oskar,
Did you get your example to work? When I use the docs like you have I just get "Logger does not contain a definition for Info". Which is true, it's "information" but then the syntax for the rest seems wrong.
I'm trying to use it in my own class in a V8.6.1 .core project (as opposed to .web).
Any advice would be great.
/ Craig
Hi Craig,
Are you trying to create your own logging or use the Core Umbraco logging facilities?
If it is the latter and you are taking advantage of Dependency Injection, if you inject ILogger or IProfilingLogger (both are in the Umbraco.Core.Logging name space) then you can access them and log things as and when you need.
Both have .Info, .Warning, and .Error methods on them where you can pass the relevant information to the methods to be logged.
Nik
Hi Nik,
Thanks for your reply. I'm trying to use the Core Umbraco logging facilities. Although I've read and read about DI and understand what it's meant to do, I haven't yet managed to get my head around the syntax, it just doesn't say anything to me.
The class is "using Umbraco.Core.Logging;" but on
Logger.Info<ImportProperties>("Error Saving Property ID: {PropertyId}", apiProperty.property_ID);
I just get "Logger does not exist in the current context". Do I "new" something or what is the syntax to DI it? It's so confusing.If I use the DI syntax from the Docs then I get "Logger does not contain a definition for Info".
I'm "just" updating a class called by an API from V7 to V8 ;)
The V7 code which worked was
LogHelper.Error(MethodBase.GetCurrentMethod().DeclaringType, "Error Saving Property ID: " + property.Property_ID, ex);
/Craig
Oskar,
Did you or anyone else find an approach to this issue?
Like Craig100, I too am struggling to wrap my mind around DI, especially in the context of moving from an ApplicationEventHandler in v7.4.2 to a Composer / Component in v8.6.2. Ultimately, my goal is to hook into Umbraco's messaging pipeline to display a custom message in the Backoffice when a user publishes a node that doesn't meet certain business rules.
I guess the question I'm left with is "how do I get a handle on whatever Logger Umbraco is currently using that implements ILogger?"
Here's a snippet of my DelegatingHandler, as is from the v7.4.2 implementation:
Here's where I get stuck in my Composer in the v8.6.2 implementation:
What do I pass to .Add()? I know I can create an empty constructor and create a brand new instance of Serilog, but doesn't that defeat the purpose of DI?
is working on a reply...