Coder Social home page Coder Social logo

raisedapp / hangfire.jobslogger Goto Github PK

View Code? Open in Web Editor NEW
34.0 4.0 4.0 1.15 MB

A Hangfire extension to store a log during job execution.

Home Page: https://www.nuget.org/packages/Hangfire.JobsLogger

License: MIT License

C# 93.50% HTML 6.24% ASP.NET 0.26%
hangfire hangfire-extension logging recurring-job nuget

hangfire.jobslogger's Introduction

Hangfire.JobsLogger

NuGet Actions Status Master Actions Status Develop Official Site License MIT

Overview

A Hangfire extension to store a log during job execution

joblogshistory

Installation

Install a package from Nuget.

Install-Package Hangfire.JobsLogger

Usage

DotNetCore

For service side:

services.AddHangfire(config => config.UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"))
                                                 .UseJobsLogger();

NetFramework

For startup side:

GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireConnection").UseJobsLogger();

Example

using Hangfire.JobsLogger;

RecurringJob.AddOrUpdate(() => taskExample.TaskMethod(null), Cron.Minutely);

//...

private readonly ILogger _log = ApplicationLogging.CreateLogger<TaskExample>();

public void TaskMethod(PerformContext context)
{
  var jobId = context.BackgroundJob.Id;

  foreach (int i in Enumerable.Range(1, 10)) 
  {
    context.LogTrace($"{i} - Trace Message.. {DateTime.UtcNow.Ticks}");
    context.LogDebug($"{i} - Debug Message.. {DateTime.UtcNow.Ticks}");
    context.LogInformation($"{i} - Information Message.. {DateTime.UtcNow.Ticks}");
    context.LogWarning($"{i} - Warning Message.. {DateTime.UtcNow.Ticks}");
    context.LogError($"{i} - Error Message.. {DateTime.UtcNow.Ticks}");
    context.LogCritical($"{i} - Critical Message.. {DateTime.UtcNow.Ticks}");

    //Traditional ILogger Usage
    _log.LogTrace(jobId: jobId, $"{i} - Trace Message.. {DateTime.UtcNow.Ticks}");
    _log.LogDebug(jobId: jobId, $"{i} - Debug Message.. {DateTime.UtcNow.Ticks}");
    _log.LogInformation(jobId: jobId, $"{i} - Information Message.. {DateTime.UtcNow.Ticks}");
    _log.LogWarning(jobId: jobId, $"{i} - Warning Message.. {DateTime.UtcNow.Ticks}");
    _log.LogError(jobId: jobId, $"{i} - Error Message.. {DateTime.UtcNow.Ticks}");
    _log.LogCritical(jobId: jobId, $"{i} - Critical Message.. {DateTime.UtcNow.Ticks}");
  }
}

Note Hangfire is responsible for injecting an instance of the PerformContext class.

The logs can be consulted in the detail of the logs on the enqueued state card: jobdetail

Options

In the UseJobsLogger method you can use an instance of the Hangfire.JobsLogger.JobsLoggerOptions class to specify some options of this plugin.

Below is a description of them:

Option Description Default Value
LogLevel Set the log level to be stored in hangfire Microsoft.Extensions.Logging.LogLevel.Trace
LogTraceColor Color that will be used to display log messages of this type System.Drawing.Color.LightGreen
LogDebugColor Color that will be used to display log messages of this type System.Drawing.Color.DarkGreen
LogInformationColor Color that will be used to display log messages of this type System.Drawing.Color.Blue
LogWarningColor Color that will be used to display log messages of this type System.Drawing.Color.DarkOrange
LogErrorColor Color that will be used to display log messages of this type System.Drawing.Color.Red
LogCriticalColor Color that will be used to display log messages of this type System.Drawing.Color.DarkRed

Credits

  • Brayan Mota
  • Lucas Ferreras

Thanks

This project would not have been possible, without the collaboration of the following projects:

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

paypal

License

This project is under MIT license. You can obtain the license copy here.

hangfire.jobslogger's People

Contributors

bamotav avatar boukeversteegh avatar felixclase avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

hangfire.jobslogger's Issues

Log is always empty

I'm using SeriLog to write logs to a file. So I added this library and append the

configuration.UseJobsLogger();

But no result is displayed in the log tab. I didn't understand if I can't continue to use a external provider like SeriLog and replace it by this or if this can work simultaneously and I'm doing something wrong.

Add UI - Jobs Logger

Add the Logs menu and display the Logs messages that have been registered by the plugin.

The logging is empty

Hi, I am using .Net core and implement this plugin.
services.AddHangfire(config => { config.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")).UseJobsLogger(); config.UseMediatR(); });
This is my code in the Startup.cs

`private readonly ILogger _logger;

public TestLogging(ILogger logger)
{
_logger = logger
}

public void TestMethod()
{
context.LogInformation($"{i} - Information Message.. {DateTime.UtcNow.Ticks}");
}`

image

this is my code. The logging is empty.
Note: I using Nlog in this project

The given key was not present in the dictionary.

It seems that this library is having the same behavior as the "Hangfire.Storage.SQLite.SQLiteMonitoringApi" where you are trying to access keys in the dictionary that are not present.

HEre is an example:

"error": {
"code": "ExceptionErrorCode",
"message": "The given key 'Queue' was not present in the dictionary.",
"target": "System.Collections.Generic.KeyNotFoundException",
"details": [
{
"code": "ExceptionStacktraceErrorCode",
"message": " at System.Collections.Generic.Dictionary2.get_Item(TKey key)\r\n at Hangfire.JobsLogger.Dashboard.EnqueuedStateRenderer.Render(HtmlHelper helper, IDictionary2 stateData)\r\n at Hangfire.Dashboard.Pages.JobDetailsPage.Execute()\r\n at Hangfire.Dashboard.RazorPage.TransformText(String body)\r\n at Hangfire.Dashboard.RazorPageDispatcher.Dispatch(DashboardContext context)\r\n at Hangfire.Dashboard.AspNetCoreDashboardMiddleware.Invoke(HttpContext httpContext)\r\n at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)\r\n at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)\r\n at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)\r\n at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIIndexMiddleware.Invoke(HttpContext httpContext)\r\n at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext)\r\n at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)\r\n at Application.Core.AspNetCore.Errors.ErrorHandlerMiddleware.Invoke(HttpContext context, ILogger`1 logger, IHostingEnvironment env)",
"details": []
}
]
}
}

How to determine if the JobsLogger is working

After looking a the Example PNG files, I was able to determine that the JobLogger is embedded in the Dashboard "Jobs".

I had to drill down into the Jobs to find the details and logs.

THANK YOU for developing this! It will make my day easier.

RC

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.