AutoLog 1.0.0

dotnet add package AutoLog --version 1.0.0
NuGet\Install-Package AutoLog -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AutoLog" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutoLog --version 1.0.0
#r "nuget: AutoLog, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AutoLog as a Cake Addin
#addin nuget:?package=AutoLog&version=1.0.0

// Install AutoLog as a Cake Tool
#tool nuget:?package=AutoLog&version=1.0.0

ITomoLog & ITomoTask Library

Serilog custom library

GitHub repo size GitHub contributors GitHub stars GitHub forks Twitter Follow

I: Thêm thư viện vào dự án : (Update)

Sử dụng Nuget:

dotnet add package ITomoTask --version 1.0.2
dotnet add package ITomoLog --version 1.0.1

II: Sử dụng :

1: Trong file Program.cs thêm thư viện SelogLib:

a: thêm namespace :

using ITomoLog;
using ITomoTask;

b: thêm dòng lệnh :

TomoLog.Load(builder); // Nạp dịch vụ serilog
TomoTask.Load(builder);  // nạp dịch vụ Quartz lập lịch tự động ghi log vào DB

vào sau dòng lệnh :

var builder = WebApplication.CreateBuilder(args);

c: thêm dòng lệnh :

app.UseMiddleware(typeof(ExceptionHandlingMiddlewareOfSelog));  //thêm Middleware custom để bắt ngoại lệ và ghi log theo cấu hình chung.

vào sau dòng lệnh :

var app = builder.Build();

2: Để ghi log thì thêm thư viện như bước trên , sau đó sử dụng :

TomoLog.WLog<Class_name>("Nội dung log",Cấp độ log); // ex: TomoLog.WLog<WeatherForecastController>("WeatherForecast not start", Level.Error);

hoặc :

TomoLog.WLog<Class_name>(ngoại lệ,"Nội dung log",Cấp độ log); 

ex:

catch (Exception ex)
{
    TomoLog.WLog<MathController>(ex,ex.Message,Level.Error);
    return 0;
}

III : Cấu hình ghi log mở file "appsettings.json"

Hướng dẫn: Copy nội dung trong file "appsettings.json" chèn thay cho đoạn code mặc định trong file cùng tên nằm ở thư mục gốc của project. - chèn thay cho đoạn code mặc định bên dưới:

"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },

1: Cấu hình cơ sở dữ liệu để ghi log, lựa chọn dùng csdl nào thì nhập tên ở "SelectDB" :

"Sqlsv": {
"ketnoi1": "Data Source=your_host,1433;Initial Catalog=your_database;User ID=your_id;Password=your_password;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=false",
"ketnoi2": "Data Source=your_host,1433;Initial Catalog=your_database;User ID=your_id;Password=password;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=false"
},

"MongoDb": {
  "server": "mongodb://user:password@localhost:27017",  
  "database": "your_database",
  "collection": "your_collection"
},

"SelectDB": "MongoDb", //neu chon sqlserver thi nhap:   "Sqlsv:ketnoi2"

2: Cấu hình đọc tên tệp log, tên này sẽ tương ứng với tên đặt cho file json ở cấu hình mục 3 :

"social": "hs_tiki",  // tien to file log , ex: hs_tiki_20230930.json

3: Cấu hình ghi log vào file Json :

  "Name": "File",
  "Args": {
    "path": "Logs/hs_tiki_.json",
    "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
    "rollingInterval": "Day",
    "retainedFileCountLimit": 7
  }

4: Cấp độ ghi thấp nhất đang để là Debug , các log từ hệ thống là Warning :

"MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning",
        "Microsoft.Hosting.Lifetime": "Warning"
      }
    }

5: Chia hệ thống ghi log làm 2 phần riêng theo cấp độ khác nhau, mỗi phần sẽ có 1 file log :

"Filter": [
  {
    "Name": "ByIncludingOnly",
    "Args": {
      "expression": "(@Level = 'Error' or @Level = 'Fatal' or @Level = 'Warning')"
    }
  }
]


"Filter": [
  {
    "Name": "ByIncludingOnly",
    "Args": {
      "expression": "(@Level = 'Information' or @Level = 'Debug')"
    }
  }
]

6: Cấu hình ghi log vào file văn bản, mặc định không dùng:

 "Name": "File",
 "Args": {
   "path": "./logs/RestApiLog.log",
   "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz},[{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
   "rollOnFileSizeLimit": true,
   "fileSizeLimitBytes": 4194304,
   "retainedFileCountLimit": 15,
   "rollingInterval": "Minute"
 }

7: Cấu hình ghi log vào Seq (tùy chọn thêm):

  "Name": "Seq",
  "Args": {
    "ServerUrl": "http://localhost:5341",
    "ApiKey": "zOrnBW2oEL9AmYoPig0n"
  }

8: Cấu hình ghi log vào Bot telegram (Chỉ sử dụng cho các log quan trọng):

 "Name": "TeleSink",
 "Args": {
   "telegramApiKey": "your_API key",
   "telegramChatId": "your_chat_Id"
 }

IV: Hướng dẫn chi tiết tạo bot telegram :

Bước 1: Truy cập vào Telegram.

Bước 2: Nhập Botfather trên thanh tìm kiếm.

Bước 3: Chọn vào Botfather có tích xanh.

image

Bước 4: Nhấn vào Start.

Bước 5: Đoạn chat được hiển thị > Nhấn vào phần /newbot – create a new bot.

Bước 6: Điền tên bạn mong muốn cho Bot.

image

Bước 7: Nhấn nút Gửi

Bước 8: Điền tên người dùng cho Bot.

Lưu ý: Tên bạn muốn tạo cho bot phải có đuôi kết thúc bằng chữ “bot”, ví dụ như isFngBot hoặc isFng_bot.

Bước 9: Nhấn nút Gửi

image

Bước 10: Hệ thống gửi thông báo thành công xác nhận thành công.

Bước 11: copy API mà Botfather cung cấp và lưu trữ cẩn thận.

Bước 12: Tạo 1 public channel rồi add bot vào đó và set bot làm adminstrators.

Bước 13: Copy tên channel và API key ở trên để cấu hình trong Serilog.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 105 1/30/2024