Skip to content

Commit

Permalink
Se actualiza a la ultima versión IzyPro#126 from IzyPro/v1.4.10
Browse files Browse the repository at this point in the history
  • Loading branch information
hramirez committed Feb 16, 2024
1 parent 9a940f7 commit b6b91c6
Show file tree
Hide file tree
Showing 32 changed files with 825 additions and 272 deletions.
66 changes: 43 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

[![WatchDog](https://img.shields.io/badge/WatchDog-blueviolet)](https://github.com/IzyPro/WatchDog)
[![Version](https://img.shields.io/nuget/vpre/WatchDog.NET?color=orange)](https://www.nuget.org/packages/WatchDog.NET#versions-tab)
[![Downloads](https://img.shields.io/nuget/dt/WatchDog.NET?color=red)](https://www.nuget.org/packages/WatchDog.NET#versions-tab)
[![MIT License](https://img.shields.io/github/license/IzyPro/WatchDog?color=Green)](https://github.com/IzyPro/WatchDog/blob/main/LICENSE)
[![WatchDog](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2FIzyPro%2FWatchDog)](https://twitter.com/intent/tweet?hashtags=WatchDog&original_referer=https%3A%2F%2Fdeveloper.twitter.com%2F&ref_src=twsrc%5Etfw%7Ctwcamp%5Ebuttonembed%7Ctwterm%5Eshare%7Ctwgr%5E&related=twitterapi%2Ctwitter&text=Hello%2C%20world!%0DCheck%20out%20this%20awesome%20developer%20tool&url=https%3A%2F%2Fgithub.com%2FIzyPro%2FWatchDog&via=WatchDog)
[![WatchDog](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2FIzyPro%2FWatchDog)](https://twitter.com/intent/tweet?hashtags=WatchDog&original_referer=https%3A%2F%2Fdeveloper.twitter.com%2F&ref_src=twsrc%5Etfw%7Ctwcamp%5Ebuttonembed%7Ctwterm%5Eshare%7Ctwgr%5E&related=twitterapi%2Ctwitter&text=Hello%2C%20world!%0DCheck%20out%20this%20awesome%20developer%20tool&url=https%3A%2F%2Fgithub.com%2FIzyPro%2FWatchDog&via=HQWatchdog)

## Introduction

WatchDog is a Realtime Message, Event, HTTP (Request & Response) and Exception logger and viewer for ASP.Net Core Web Apps and APIs. It allows developers log and view messages, events, http requests made to their web application and also exception caught during runtime in their web applications, all in Realtime.
It leverages `SignalR` for real-time monitoring and `LiteDb` a Serverless MongoDB-like database with no configuration with the option of using your external MSSQL, MySQl or Postgres databases.
It leverages `SignalR` for real-time monitoring and `LiteDb` a Serverless MongoDB-like database with no configuration with the option of using your external databases (MSSQL, MySQl, Postgres, MongoDB).

# ![Request & Response Viewer](https://github.com/IzyPro/WatchDog/blob/main/watchlog.png)

## General Features

- RealTime HTTP Request and Response Logger
- RealTime Exception Logger
- RealTime HTTP Request, Response, and Exception Logger
- In-code message and event logging
- User Friendly Logger Views
- Search Option for HTTP and Exception Logs
Expand All @@ -26,17 +26,13 @@ It leverages `SignalR` for real-time monitoring and `LiteDb` a Serverless MongoD

## What's New

- Log Level (Info, Error, Warning)
- Extra AutoClear Durations (Hourly, Every6Hours, Every12Hours)
- Optimized Queries
- Cors Support
- Global Json Serializer Support
- Security Optimization
- Query Filters Fixes and Optimizations
- Package Assembly as DB Name Fix(MongoDB)

## Fixes
### Breaking Changes

- Postgres byte error
- OutOfRangeException
- Embedded DB Memory Release on Clear Logs
- SqlDriverOption is now DbDriverOption (>= 1.4.0)


## Support
Expand All @@ -47,12 +43,12 @@ It leverages `SignalR` for real-time monitoring and `LiteDb` a Serverless MongoD
Install via .NET CLI

```bash
dotnet add package WatchDog.NET --version 1.3.3
dotnet add package WatchDog.NET --version 1.4.10
```
Install via Package Manager

```bash
Install-Package WatchDog.NET --version 1.3.3
Install-Package WatchDog.NET --version 1.4.10
```


Expand Down Expand Up @@ -91,15 +87,15 @@ services.AddWatchDogServices(opt =>
});
```

### Setup Logging to External Db (MSSQL, MySQL & PostgreSQL) `Optional`
Add Database Connection String and Choose SqlDriver Option
### Setup Logging to External Db (MSSQL, MySQL, PostgreSQL & MongoDb) `Optional`
Add Database Connection String and Choose DbDriver Option

```c#
services.AddWatchDogServices(opt =>
{
opt.IsAutoClear = false;
opt.SetExternalDbConnString = "Server=localhost;Database=testDb;User Id=postgres;Password=root;";
opt.SqlDriverOption = WatchDogSqlDriverEnum.PostgreSql;
opt.DbDriverOption = WatchDogSqlDriverEnum.PostgreSql;
});
```

Expand All @@ -123,14 +119,14 @@ app.UseWatchDog(opt =>


>**NOTE**
> If your project uses authentication, then `app.UseWatchDog();` should come before app.UseRouting(), app.UseAuthentication(), app.UseAuthorization(), in that order
> If your project uses authentication, then `app.UseWatchDog();` should come after app.UseRouting(), app.UseAuthentication(), app.UseAuthorization(), in that order
<!--- >If your projects startup or program class contains app.UseMvc() or app.UseRouting() then app.UseWatchDog() should come after `Important`
>If your projects startup or program class contains app.UseEndpoints() then app.UseWatchDog() should come before `Important` -->
# ![Request and Response Sample Details](https://github.com/IzyPro/WatchDog/blob/main/requestLog.png)

#### Optional Configurations: `Optional`
- Blacklist: List of routes, paths or specific strings to be ignored (should be a comma separated string like below).
- Blacklist: List of routes, paths or endpoints to be ignored (should be a comma separated string like below).
- Serializer: If not default, specify the type of global json serializer/converter used
- CorsPolicy: Policy Name if project uses CORS

Expand All @@ -140,7 +136,7 @@ app.UseWatchDog(opt =>
opt.WatchPageUsername = "admin";
opt.WatchPagePassword = "Qwerty@123";
//Optional
opt.Blacklist = "Test/testPost, weatherforecast"; //Prevent logging for specified endpoints
opt.Blacklist = "Test/testPost, api/auth/login"; //Prevent logging for specified endpoints
opt.Serializer = WatchDogSerializerEnum.Newtonsoft; //If your project use a global json converter
opt.CorsPolicy = "MyCorsPolicy"
});
Expand Down Expand Up @@ -168,12 +164,32 @@ app.UseWatchDog(opt =>
```
### Log Messages/Events
```
WatchLogger.Log("...TestGet Started...");
WatchLogger.Log("...Test Log...");
WatchLogger.LogWarning(JsonConvert.Serialize(model));
WatchLogger.LogError(res.Content);
WatchLogger.LogError(res.Content, eventId: reference);
```

# ![In-code log messages](https://github.com/IzyPro/WatchDog/blob/main/in-code.png)

#### Sink Logs from ILogger
You can also sink logs from the .NET ILogger into WatchDog

For .NET 6 and above
```
builder.Logging.AddWatchDogLogger();
```
For .NET Core 3.1, configure logging and add `.AddWatchDogLogger()` to the `CreateHostBuilder` method of the `Program.cs` class
```
Host.CreateDefaultBuilder(args)
.ConfigureLogging( logging =>
{
logging.AddWatchDogLogger();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
```

### View Logs and Exception
Start your server and head to `/watchdog` to view the logs.
Expand All @@ -192,3 +208,7 @@ Alternatively, open an issue and we'll get to it as soon as we can.
Kelechi Onyekwere - [Github](https://github.com/Khelechy) [Twitter](https://twitter.com/khelechy1337)

Israel Ulelu - [Github](https://github.com/IzyPro) [Twitter](https://twitter.com/IzyPro_)



### [Official Documentation](https://watchdog-3.gitbook.io/watchdog)
11 changes: 8 additions & 3 deletions WatchDog/WatchDog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<Description>WatchDog is a Realtime Message, Event, HTTP (Request &amp; Response) and Exception logger and viewer for ASP.Net Core Web Apps and APIs. It allows developers log and view messages, events, http requests made to their web application and also exception caught during runtime in their web applications, all in Realtime. It leverages SignalR for real-time monitoring and LiteDb a Serverless MongoDB-like database with no configuration with the option of using your external MSSQL, MySQl or Postgres databases.</Description>
<Description>WatchDog is a Realtime Message, Event, HTTP (Request &amp; Response) and Exception logger and viewer for ASP.Net Core Web Apps and APIs. It allows developers log and view messages, events, http requests made to their web application and also exception caught during runtime in their web applications, all in Realtime. It leverages SignalR for real-time monitoring and LiteDb a Serverless MongoDB-like database with no configuration with the option of using your external databases.</Description>
<PackageProjectUrl>https://github.com/IzyPro/WatchDog</PackageProjectUrl>
<PackageIcon>favicon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand All @@ -12,9 +12,13 @@
<Authors>Israel Ulelu, Kelechi Onyekwere</Authors>
<PackageId>WatchDog.NET</PackageId>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>1.3.3-rc</Version>
<PackageReleaseNotes>- Query Optimization, Log Level (Info, Error, Warning), [Fix] Postgres Byte error</PackageReleaseNotes>
<Version>1.4.10</Version>
<PackageReleaseNotes>- Security Optimization
- Query Filters Fixes and Optimizations
- Package Assembly as DB Name Fix(MongoDB)</PackageReleaseNotes>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -51,6 +55,7 @@
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="3.1.22" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.22" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.2.0" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
<PackageReference Include="MySql.Data" Version="8.0.29" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Npgsql" Version="[5.0.0,5.0.10]" Condition="'$(TargetFramework)' == 'netcoreapp3.1'" />
Expand Down
34 changes: 27 additions & 7 deletions WatchDog/WatchDogExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using System;
Expand Down Expand Up @@ -32,26 +33,44 @@ public static IServiceCollection AddWatchDogServices(this IServiceCollection ser
AutoClearModel.IsAutoClear = options.IsAutoClear;
AutoClearModel.ClearTimeSchedule = options.ClearTimeSchedule;
WatchDogExternalDbConfig.ConnectionString = options.SetExternalDbConnString;
WatchDogSqlDriverOption.SqlDriverOption = options.SqlDriverOption;
WatchDogDatabaseDriverOption.DatabaseDriverOption = options.DbDriverOption;
WatchDogExternalDbConfig.MongoDbName = Assembly.GetCallingAssembly().GetName().Name?.Replace('.', '_') + "_WatchDogDB";

if (!string.IsNullOrEmpty(WatchDogExternalDbConfig.ConnectionString) && WatchDogSqlDriverOption.SqlDriverOption == 0)
throw new WatchDogDBDriverException("Missing DB Driver Option: SQLDriverOption is required at .AddWatchDogServices()");
if (WatchDogSqlDriverOption.SqlDriverOption != 0 && string.IsNullOrEmpty(WatchDogExternalDbConfig.ConnectionString))
if (!string.IsNullOrEmpty(WatchDogExternalDbConfig.ConnectionString) && WatchDogDatabaseDriverOption.DatabaseDriverOption == 0)
throw new WatchDogDBDriverException("Missing DB Driver Option: DbDriverOption is required at .AddWatchDogServices()");
if (WatchDogDatabaseDriverOption.DatabaseDriverOption != 0 && string.IsNullOrEmpty(WatchDogExternalDbConfig.ConnectionString))
throw new WatchDogDatabaseException("Missing connection string.");

services.AddDistributedMemoryCache();

services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(5);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});

services.AddSignalR();
services.AddMvcCore(x =>
{
x.EnableEndpointRouting = false;
}).AddApplicationPart(typeof(WatchDogExtension).Assembly);

services.AddSingleton<IBroadcastHelper, BroadcastHelper>();
services.AddSingleton<IMemoryCache, MemoryCache>();

if (!string.IsNullOrEmpty(WatchDogExternalDbConfig.ConnectionString))
{
ExternalDbContext.Migrate();
if (WatchDogDatabaseDriverOption.DatabaseDriverOption == src.Enums.WatchDogDbDriverEnum.Mongo)
{
ExternalDbContext.MigrateNoSql();
}
else
{
ExternalDbContext.Migrate();
}
}


if (AutoClearModel.IsAutoClear)
services.AddHostedService<AutoLogClearerBackgroundService>();

Expand All @@ -60,7 +79,6 @@ public static IServiceCollection AddWatchDogServices(this IServiceCollection ser

public static IApplicationBuilder UseWatchDogExceptionLogger(this IApplicationBuilder builder)
{

return builder.UseMiddleware<src.WatchDogExceptionLogger>();
}

Expand Down Expand Up @@ -95,6 +113,8 @@ public static IApplicationBuilder UseWatchDog(this IApplicationBuilder app, Acti

app.UseAuthorization();

app.UseSession();

if (!string.IsNullOrEmpty(options.CorsPolicy))
app.UseCors(options.CorsPolicy);

Expand Down
17 changes: 17 additions & 0 deletions WatchDog/WatchDogLoggerExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace WatchDog
{
public static class WatchDogLoggerExtension
{
//
// Summary:
// Uses built in ILogger. Note: caller info has an overhead of about <= 4ms
public static ILoggingBuilder AddWatchDogLogger(this ILoggingBuilder builder, bool logCallerInfo = true, bool log = true)
{
builder.Services.AddSingleton<ILoggerProvider, WatchDogLoggerProvider>(_ => new WatchDogLoggerProvider(log, logCallerInfo));
return builder;
}
}
}
37 changes: 36 additions & 1 deletion WatchDog/src/Controllers/WatchPageController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Linq;
using System.Threading.Tasks;
using WatchDog.src.Filters;
using WatchDog.src.Helpers;
using WatchDog.src.Managers;
using WatchDog.src.Models;
Expand All @@ -10,23 +15,33 @@ namespace WatchDog.src.Controllers
[AllowAnonymous]
public class WatchPageController : Controller
{
public WatchPageController()
{

}

[CustomAuthenticationFilter]
public async Task<JsonResult> Index(string searchString = "", string verbString = "", string statusCode = "", int pageNumber = 1)
{
var result = await DynamicDBManager.GetAllWatchLogs(searchString, verbString, statusCode, pageNumber);
return Json(new { PageIndex = result.PageIndex, TotalPages = result.TotalPages, HasNext = result.HasNextPage, HasPrevious = result.HasPreviousPage, logs = result.Data }, GeneralHelper.CamelCaseSerializer);
}

[CustomAuthenticationFilter]
public async Task<JsonResult> Exceptions(string searchString = "", int pageNumber = 1)
{
var result = await DynamicDBManager.GetAllWatchExceptionLogs(searchString, pageNumber);
return Json(new { PageIndex = result.PageIndex, TotalPages = result.TotalPages, HasNext = result.HasNextPage, HasPrevious = result.HasPreviousPage, logs = result.Data }, GeneralHelper.CamelCaseSerializer);
}

[CustomAuthenticationFilter]
public async Task<JsonResult> Logs(string searchString = "", string logLevelString = "", int pageNumber = 1)
{
var result = await DynamicDBManager.GetAllLogs(searchString, logLevelString, pageNumber);
return Json(new { PageIndex = result.PageIndex, TotalPages = result.TotalPages, HasNext = result.HasNextPage, HasPrevious = result.HasPreviousPage, logs = result .Data}, GeneralHelper.CamelCaseSerializer);
return Json(new { PageIndex = result.PageIndex, TotalPages = result.TotalPages, HasNext = result.HasNextPage, HasPrevious = result.HasPreviousPage, logs = result.Data }, GeneralHelper.CamelCaseSerializer);
}

[CustomAuthenticationFilter]
public async Task<JsonResult> ClearLogs()
{
var cleared = await DynamicDBManager.ClearLogs();
Expand All @@ -40,12 +55,32 @@ public JsonResult Auth(string username, string password)

if (username.ToLower() == WatchDogConfigModel.UserName.ToLower() && password == WatchDogConfigModel.Password)
{
HttpContext.Session.SetString("isAuth", "true");
return Json(true);
}
else
{
return Json(false);
}
}

public JsonResult LogOut()
{
HttpContext.Session.Remove("isAuth");
return Json(true);
}

public JsonResult IsAuth()
{

if (!HttpContext.Session.TryGetValue("isAuth", out var isAuth))
{
return Json(false);
}
else
{
return Json(true);
}
}
}
}
Loading

0 comments on commit b6b91c6

Please sign in to comment.