Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting-started-with-ASP.NET-Core vs2017 #157

Closed
NelsonRothermel opened this issue Jun 21, 2017 · 27 comments · Fixed by #180
Closed

Getting-started-with-ASP.NET-Core vs2017 #157

NelsonRothermel opened this issue Jun 21, 2017 · 27 comments · Fixed by #180
Assignees
Labels
ASP.NET Core ASP.NET Core - all versions

Comments

@NelsonRothermel
Copy link

I followed https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(csproj---vs2017) for my VS2017 .NET Core project. Step 4 shows the following:

public Startup(IHostingEnvironment env)
{
    env.ConfigureNLog("nlog.config");
}

public void ConfigureServices(IServiceCollection Services)
{
    //call this in case you need aspnet-user-authtype/aspnet-user-identity
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}

That doesn't work for me since Startup runs before ConfigureServices. When ConfigureNLog runs I get the following error: 2017-06-20 21:39:37.7335 Warn Missing IHttpContextAccessor. Has it been registered before loading NLog Configuration? Consider reloading NLog Configuration after having registered the IHttpContextAccessor.

I fixed it by moving ConfigureNLog inside of the Configure method and taking a IHostingEnvironment parameter, but was wondering if the documentation was incorrect or if the code was intended to be used that way somehow.

@mhamri
Copy link

mhamri commented Jun 25, 2017

i moved the ConfigureNLog as you said, but still i'm getting the said error

@304NotModified 304NotModified self-assigned this Jun 26, 2017
@304NotModified
Copy link
Member

Is this on ASP.NET Core 2?

@mhamri
Copy link

mhamri commented Jun 26, 2017 via email

@304NotModified
Copy link
Member

if i log inside a class that doesn't
have action and controller, then this message will appear.

could you please give an example? I'm not fully get it, thanks!

@mhamri
Copy link

mhamri commented Jun 26, 2017

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext context, IServiceProvider services, Paths pathHelper)
{
    ILogger<Startup> logger = null;
    if (loggerFactory != null)
    {
        //loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        //loggerFactory.AddDebug();

        loggerFactory.AddNLog();


        app.AddNLogWeb();
        env.ConfigureNLog("nlog.config").Reload(); 
        /*
        i also have env.ConfigureNLog("nlog.config") at the startup constructor, 
       but still after `addNlogWeb`  i'm trying to configure nlog and reload it,
        still getting the message to load IHttpContext first
       */

       // here i'm loading the service manually
        logger = services.GetService<ILogger<Startup>>();
        logger.LogInformation($"logger started, env is {env.EnvironmentName}");
    }

    if (env.IsDevelopment() || env.IsStaging())
    {
        logger?.LogInformation("entred the development mode");
        app.UseStatusCodePages();
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
        app.UseBrowserLink();
    }


    ....

}

the log look like this :

2017-06-26 08:27:27.3724|0|TentCode.GifHouse.Startup|INFO|  logger started, env is Staging  |url: |action:   
2017-06-26 08:27:27.4534|0|TentCode.GifHouse.Startup|INFO|  entred the development mode     |url: |action:

but if i call logger in normal way that suggested by asp.net in a controller i will get log like bellow:

2017-06-26 08:27:32.5506|0|TentCode.GifHouse.Controllers.HomeController|TRACE|  a webpage requested  |url: http://localhost/ |action:  Index  
2017-06-26 08:27:47.8971|0|TentCode.GifHouse.Models.ImageCache|DEBUG|  error saving cached version  |url: http://localhost/image/Static/d47e26a5-ce29-4503-b89f-9a2483403bec_Default.jpg |action:  Index  

which is right.

so i am thinking the error is caused by empty ${aspnet-request-url} so i changed the nlog.config to this:

<target xsi:type="File" name="ownFile-web" fileName=".\logs\nlog-own-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|  ${message} ${exception} ${when:when=length('${aspnet-request-url}')>0:Inner=|url${literal:text=\:} ${aspnet-request-url}} ${when:when=length('${aspnet-mvc-action}')>0:Inner=|action${literal:text=\:}  ${aspnet-mvc-action}}  " />

@304NotModified
Copy link
Member

2017-06-26 08:27:27.3724|0|TentCode.GifHouse.Startup|INFO| logger started, env is Staging |url: |action:

This is correct? There isn't a url and page when you're in Configure?

@KolyaNET
Copy link

@304NotModified I have exactly the same problem.
When I delete the parameters containing $ {aspnet -...} then the warning disappears.
In the Startup class, I could not find a way to get the request URL.
https://stackoverflow.com/questions/28223565/how-to-get-the-request-url-on-application-startup

@mhamri
Copy link

mhamri commented Jun 29, 2017

@304NotModified at that time the IHttpContext didn't build yet, plus it is part of the application start.

@mhamri
Copy link

mhamri commented Jun 29, 2017

@KolyaNET you can't do that on startup, if you want to capture all the URL, then you need to create a middlewear

@304NotModified 304NotModified changed the title https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(csproj---vs2017) Getting-started-with-ASP.NET-Core vs2017 Jul 2, 2017
@304NotModified
Copy link
Member

I'm a bit confused. Is there something we (NLog) could do or is this resolved?

@NelsonRothermel
Copy link
Author

I resolved mine with the change on the first post but just wanted confirmation if that was intended use. I'm not sure about the others.

@vflame
Copy link

vflame commented Jul 7, 2017

ConfigureNLog() needs to go after AddNLogWeb() to avoid the warning (but this seems odd...)

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
            loggerFactory.AddNLog();
            app.AddNLogWeb();
            env.ConfigureNLog("nlog.config");
}

@textnumber085
Copy link

Got the same warnning.

2017-07-20 14:48:32.4491 Warn Missing IHttpContextAccessor. Has it been registered before loading NLog Configuration? Consider reloading NLog Configuration after having registered the IHttpContextAccessor.

@pholly
Copy link

pholly commented Jul 28, 2017

I get the same warning. My Startup.cs looks just like the Getting Started Guide. On asp.net core 1.1.2.

I tried moving env.ConfigureNLog() to the Configure function but I still get the warning.

@DanielMcAssey
Copy link

Getting the same issue, tried moving it and still getting the warning.

@304NotModified
Copy link
Member

But does it work?

@AndrewSilver
Copy link

AndrewSilver commented Aug 18, 2017

Have the same issue. Is there a solution?

I have the following code and and still have the warning

public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
...
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddNLog();
            app.AddNLogWeb();
            env.ConfigureNLog("nlog.config");
...

@grokky1
Copy link
Contributor

grokky1 commented Aug 31, 2017

I'm not sure if it helps with the problems mentioned here, but in my case, the trick was to have env.ConfigureNLog("nlog.config"); first. Then call AddNLog and AddNLogWeb.

@asulwer
Copy link

asulwer commented Sep 12, 2017

warning exists with core 2.0, cannot seem to get rid of it

@cgountanis
Copy link

Same issue with Core 2.0 this is my first time using it and I want to log to SQL but this first attempt using the WIKI example is not giving me high hopes.

@DanielDziubecki
Copy link

I have same warning with .net core 2.0. All logging fields like aspnet-* are not included in logs.

@304NotModified
Copy link
Member

304NotModified commented Sep 14, 2017

All logging fields like aspnet-* are not included in logs.

I think that's another issue.

It will be hard to fix this issue if we getting off topic.

AFAIK its only a warning and everything works. We will fix the warning but isn't critical.

Don't get me wrong. Good docs are important but currently I have to invest some time before I could update the docs. If someone has a working fix, please let me know.

@304NotModified
Copy link
Member

304NotModified commented Sep 27, 2017

I will try to fix this, this weekend. I think we have to move all config to the Main() instead of the startup for ASP.NET Core 2

@grokky1
Copy link
Contributor

grokky1 commented Sep 28, 2017

@304NotModified Yes logging config should be done in the app's Main() entry point (which is better because we can use the logger much earlier in the app's lifecycle).

@Surge001
Copy link

Surge001 commented Oct 4, 2017

Hi 304NotModified:
I see you've committed on auto register NLog.Web.AspNetCore some 2 days ago, but I do not see any incremented version of this assembly in NuGet Package manager: it is still showing 4.4.1 being the latest. I was curious if you had kindly resolved the issue, discussed in this trail? I want to make NLog my package of choice for Core 2.0 apps due to JavaScript logging integration, but this warning issue and failing to log any actual errors in my web apps is a show stopper. Do you have an ETA when the corrected mentioned assembly can be updated via NuGet Package Manager?

Thanks for the great work!

@304NotModified
Copy link
Member

Yes it's not released. Will try to finish it, this weekend. Ps feedback would help me also! (Especially for #184 and #185)

@paultechguy
Copy link

Was this fix ever released?

@snakefoot snakefoot added ASP.NET Core ASP.NET Core - all versions and removed ASP.NET Core 2 labels Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ASP.NET Core ASP.NET Core - all versions
Projects
None yet
Development

Successfully merging a pull request may close this issue.