日志表清理

虽然 Nebula.Juno 是一个通用的日志数据表清理服务,可以让你不写代码就能实现对日志表的清理,
但是有些场景下,你不希望部署这样一个服务,
那么可以使用 ClownFish 提供的API来实现同样的功能,可参考以下代码:

public class DataCleanWorker : BackgroundTask
{
    private static readonly int s_days = LocalSettings.GetInt("UserAskLogKeepDays", 10);

    public override int? SleepSeconds => 3600;

    public override bool FirstRun => true;

    public override void Execute()
    {
        CleaningOption option = new CleaningOption {
            DbConfig = MyConfig.DbConfig,
            TableName = "user_ask_log",
            TimeFieldName = "ask_time",
            HoursAgo = 24 * s_days  //  保留10天的日志
        };

        DataCleaner cleaner = new DataCleaner(option);

        try {
			// 执行日志清理动作
            cleaner.Execute();
        }
        finally {
            OprLog log = this.Context.OprLog;
            if( log != null ) {
                log.Text1 = cleaner.GetLogs();
            }
        }
    }
}



核心代码:

CleaningOption option = new CleaningOption { .... };
DataCleaner cleaner = new DataCleaner(option);
cleaner.Execute();