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

Dashboard does not list requests when the controller has the Authorize attribute. #33

Closed
danielts90 opened this issue Jan 21, 2021 · 10 comments
Labels
SDK-AspNetCore Related to .NET Core framework

Comments

@danielts90
Copy link

I am implementing kisslog in an API that I am developing and I am having a problem that I cannot say if it is a bug or if it is a problem with my implementations.
The problem is that when a controller method uses the Authorize attribute, the physical log is generated but the request is not listed on the dashboard.

It's OK
image
image

It's not OK
image

@catalingavan
Copy link
Owner

catalingavan commented Jan 21, 2021

Hi Daniel,

Can you please share from the Startup.cs file the Configure(IApplicationBuilder app) method?

@catalingavan catalingavan added the SDK-AspNetCore Related to .NET Core framework label Jan 21, 2021
@danielts90
Copy link
Author

Yes,
I divided Startup into several classes,
if you need any more, let me know.

LogStartup.txt
Startup.txt

@catalingavan
Copy link
Owner

catalingavan commented Jan 21, 2021

In AspNetCore the order of the Middleware is important, and I suspect this might be the issue.

I will need a copy of the physical log file which works ok, and the KissLog Internal Logs (you can save them to a StringBuilder):

static StringBuilder sb = new StringBuilder();

options.InternalLog = (message) =>
{
    sb.AppendLine(message);
};

image

To simplify the code, can you temporary change the Startup.cs to the following, and see if this makes a difference?

I copied the LogStartup.cs into the Startup.cs, and I also added the app.UseAuthorization(); and app.UseAuthentication();.

namespace Spx.Cor.Exportacao.Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuracao = configuration;
        }

        public IConfiguration Configuracao { get; }

        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        public void ConfigureServices(IServiceCollection servicos)
        {
            servicos.AddCorsCustomizado(Configuracao);

            servicos.AddDommelCustomizado();

            servicos.AddMemoryCache();

            servicos.AddHealthChecksCustomizado(Configuracao);

            servicos.AddAutoMapperCustomizado();

            servicos.AddAutenticacaoCustomizada(Configuracao);

            servicos.AddMvcCustomizado();

            servicos.AddSwaggerCustomizado(Configuracao);

            servicos.AddDICustomizado();

            servicos.AddLogCustomizado(Configuracao);
        }

        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseCorsCustomizado(env);

            app.UseExcecaoCustomizada();

            app.UseSwaggerCustomizado();

            app.UseHealthChecksCustomizado();

            app.UseMvcCustomizado();

            app.UseStaticFiles();
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseSession();

	    app.UseKissLogMiddleware(options => {
                ConfigureKissLog(options, Configuracao);
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
		
	private static void ConfigureKissLog(IOptionsBuilder options, IConfiguration configuracao)
        {
            // optional KissLog configuration
            options.Options
                .AppendExceptionDetails((Exception ex) =>
                {
                    StringBuilder sb = new StringBuilder();

                    if (ex is System.NullReferenceException nullRefException)
                    {
                        sb.AppendLine("Important: check for null references");
                    }

                    return sb.ToString();
                });

            // KissLog internal logs
            options.InternalLog = (message) =>
            {
                Debug.WriteLine(message);
            };

            RegisterKissLogListeners(options, configuracao);
        }

        private static void RegisterKissLogListeners(IOptionsBuilder options, IConfiguration configuracao)
        {
            // multiple listeners can be registered using KissLogConfiguration.Listeners.Add() method

            // Register KissLog.net cloud listener
            options.Listeners.Add(new RequestLogsApiListener(new Application(
                configuracao["KissLog.OrganizationId"],
                configuracao["KissLog.ApplicationId"])
            )
            {
                ApiUrl = configuracao["KissLog.ApiUrl"]
            });

            // Register local text files listener
            options.Listeners.Add(new LocalTextFileListener(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs"))
            {
                FlushTrigger = FlushTrigger.OnMessage
            });
        }
    }
}

@danielts90
Copy link
Author

danielts90 commented Jan 21, 2021

The Kisslog log shows an invalid parameters error

'KissLog' 2021-01-21T13:42:14Z [Trace] RequestLogsApiListener: OnFlush begin
'KissLog' 2021-01-21T13:42:14Z [Trace] POST https://api.kisslog.net/api/public/v1.0/createRequestLog begin
'KissLog' 2021-01-21T13:42:14Z [Trace] RequestLogsApiListener: OnFlush complete
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 1518.9013ms 200 application/json; charset=utf-8
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2021-01-21T13:42:13.0415767Z","tags":{"ai.application.ver":"1.0.0.0","ai.cloud.roleInstance":"001-TBL-1086","ai.user.id":"MYr1da/mmvtyAhvCeWhWHe","ai.operation.id":"9f5866b6dade774bb0edc404aaa34ab0","ai.operation.name":"GET Emails/GetEmail [version]","ai.location.ip":"::1","ai.internal.sdkVersion":"aspnet5c:2.13.1","ai.internal.nodeName":"001-TBL-1086.supermix.com.br"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"ef1c8ab28c87c843","name":"GET Emails/GetEmail [version]","duration":"00:00:01.5521875","success":true,"responseCode":"200","url":"https://localhost:44307/v1/Email/getEmail?email=teste%40teste.com","properties":{"DeveloperMode":"true","_MS.ProcessedByMetricExtractors":"(Name:'Requests', Ver:'1.1')","AspNetCoreEnvironment":"loc"}}}}
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.RemoteDependency","time":"2021-01-21T13:42:14.5443439Z","tags":{"ai.application.ver":"1.0.0.0","ai.cloud.roleInstance":"001-TBL-1086","ai.operation.id":"9f5866b6dade774bb0edc404aaa34ab0","ai.operation.parentId":"ef1c8ab28c87c843","ai.internal.sdkVersion":"rdddsc:2.13.1-12554","ai.internal.nodeName":"001-TBL-1086.supermix.com.br"},"data":{"baseType":"RemoteDependencyData","baseData":{"ver":2,"name":"POST /api/public/v1.0/createRequestLog","id":"b1742e19d1e13c49","data":"https://api.kisslog.net/api/public/v1.0/createRequestLog","duration":"00:00:00.2502000","resultCode":"400","success":false,"type":"Http","target":"api.kisslog.net","properties":{"DeveloperMode":"true","_MS.ProcessedByMetricExtractors":"(Name:'Dependencies', Ver:'1.1')","AspNetCoreEnvironment":"loc"}}}}
'KissLog' 2021-01-21T13:42:14Z [Error] POST https://api.kisslog.net/api/public/v1.0/createRequestLog 400 ERROR
Invalid parameters

image

In the request without Authorize Atribute
image

The fisical log
2021-01-21.log

@catalingavan
Copy link
Owner

Thanks. Please trigger the request again WITH the Authorization attribute on (making it fail)

@danielts90
Copy link
Author

Done
image

@catalingavan
Copy link
Owner

catalingavan commented Jan 21, 2021

Can you please try few more times? This time it should work

@danielts90
Copy link
Author

Now it's Ok
image
image

@catalingavan
Copy link
Owner

Perfect. You helped me discover a misplaced validation rule on the server, thank you!
You can change the code back as it was previously, and it should work ok now.
Sorry for that.

@danielts90
Copy link
Author

I'm so happy for contribute,
Is the first time that i open a github issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SDK-AspNetCore Related to .NET Core framework
Projects
None yet
Development

No branches or pull requests

2 participants