在Abp Vnext應(yīng)用程序中添加日志界面

abp 中雖然已經(jīng)有日志模塊,但是沒(méi)有UI界面,而一個(gè)日志界面對(duì)開(kāi)發(fā)的應(yīng)用程序來(lái)說(shuō)很重要,因此,經(jīng)過(guò)一番摸索,終于做出了自己的日志模塊,列出關(guān)鍵代碼,給有需要的朋友參考。

1、首先,在Application.Contracts項(xiàng)目中新增加一個(gè)類AuditLogDto.cs 和GetAuditLogInput.cs:
代碼塊

 public class AuditLogDto : EntityDto<Guid>
    {
        public virtual string ApplicationName { get; set; }

        public virtual Guid? UserId { get; protected set; }

        public virtual string UserName { get; protected set; }

        public virtual Guid? TenantId { get; protected set; }

        public virtual string TenantName { get; protected set; }

        public virtual Guid? ImpersonatorUserId { get; protected set; }

        public virtual Guid? ImpersonatorTenantId { get; protected set; }

        public virtual DateTime ExecutionTime { get; protected set; }

        public virtual int ExecutionDuration { get; protected set; }

        public virtual string ClientIpAddress { get; protected set; }

        public virtual string ClientName { get; protected set; }

        public virtual string ClientId { get; set; }

        public virtual string CorrelationId { get; set; }

        public virtual string BrowserInfo { get; protected set; }

        public virtual string HttpMethod { get; protected set; }

        public virtual string Url { get; protected set; }

        public virtual string Exceptions { get; protected set; }

        public virtual string Comments { get; protected set; }

        public virtual int? HttpStatusCode { get; set; }

        public virtual ICollection<EntityChange> EntityChanges { get; protected set; }

        public virtual ICollection<AuditLogAction> Actions { get; protected set; }

    }
 public class GetAuditLogInput : PagedAndSortedResultRequestDto
    {

        public DateTime StartDate { get; set; }

        public DateTime EndDate { get; set; }

        public string UserName { get; set; }

        public string ServiceName { get; set; }

        public string MethodName { get; set; }

        public string BrowserInfo { get; set; }
        public string HttpMethod { get; set; }
        public string Url { get; set; }
        public string UseName { get; set; }
        public string ApplicationName { get; set; }

        public string CorrelationId { get; set; }
        public int? MaxExecutinDuration { get; set; }
        public int? MinExecutionDuration { get; set; }
        public System.Net.HttpStatusCode? httpStutusCode { get; set; }
        public bool IncludeDetails { get; set; }

        public bool? HasException { get; set; }



    }

2、增加IAuditLogAppService.cs類

 public interface IAuditLogAppService : IApplicationService
    {
        Task<PagedResultDto<AuditLogDto>> GetListAsync(GetAuditLogInput input);
      
    }

3、在Application項(xiàng)目在增加AuditLogAppService.cs類:

public class AuditLogAppService : XXAppService, IAuditLogAppService
    {
        private readonly IAuditLogRepository _auditLogRepository;

        public AuditLogAppService(IAuditLogRepository auditLogRepository)
        {
            _auditLogRepository = auditLogRepository;
        }



        public async Task<PagedResultDto<AuditLogDto>> GetListAsync(GetAuditLogInput input)

        {
            input.Sorting = "ExecutionTime Desc";
            input.IncludeDetails = true;

            var count = await _auditLogRepository.GetCountAsync(
                input.StartDate, input.EndDate, input.HttpMethod,
                input.Url, input.UseName, input.ApplicationName, input.CorrelationId, input.MaxExecutinDuration,
                input.MinExecutionDuration, input.HasException, input.httpStutusCode).ConfigureAwait(false);
            var list = await _auditLogRepository.GetListAsync(input.Sorting,
                input.MaxResultCount, input.SkipCount, input.StartDate, input.EndDate, input.HttpMethod,
                input.Url, input.UseName, input.ApplicationName, input.CorrelationId, input.MaxExecutinDuration, input.MinExecutionDuration,
                input.HasException, input.httpStutusCode, input.IncludeDetails
                  ).ConfigureAwait(false);
          
            return new PagedResultDto<AuditLogDto>(
                count,
                ObjectMapper.Map<List<AuditLog>, List<AuditLogDto>>(list)
            );
        }

4、XXApplicationAutoMapperProfile.cs 類中增加 AutoMap類映射關(guān)系:

 public class AuditManagementApplicationAutoMapperProfile : Profile
    {
        public AuditManagementApplicationAutoMapperProfile()
        {
           
            CreateMap<AuditLog, AuditLogDto>();
        }
    }

5、web 項(xiàng)目中Pages目錄下增加AuditLogs目錄,在AuditLogs目錄下增加index.cshtml文件:

<abp-card>
    <abp-card-header>
        <abp-row>
            <abp-column size-md="_6">
                <h2>@L["AbpAuditLogs"]</h2>
            </abp-column>

        </abp-row>
    </abp-card-header>

    <abp-card-body>
        <form id="AuditLogFilterForm">
            <abp-row>
                <abp-column size-md="_3">
                    <div class="form-group">
                        <label for="StartEndRange" class="control-label">@L["DateRange"]</label>
                        <input id="StartEndRange" type="text" class="form-control date-range-picker" />
                    </div>
                </abp-column>
                <abp-column size-md="_3">
                    <abp-input asp-for="@Model.Input.UserName" label="用戶" />
                </abp-column>
                <abp-column size-md="_3">
                    <abp-select asp-for="@Model.Input.HttpStutusCode" asp-items="@Model.HttpStutusCodes" label="Http狀態(tài)碼" />
                </abp-column>
                <abp-column size-md="_3">
                    <abp-select asp-for="@Model.Input.HttpMethod" asp-items="@Model.HttpMethods" label="http方法"/>
                </abp-column>
            </abp-row>

            <abp-row>
                <abp-column size-md="_3">
                    <abp-select asp-for="@Model.Input.HasException" asp-items="@Model.HasExceptions" label="有錯(cuò)誤" />
                </abp-column>
                <abp-column size-md="_3">
                    <button id="RefreshAuditLogsButton" class="btn btn-primary"><i class="fa fa-sync"></i> @L["Refresh"]</button>
                </abp-column>

            </abp-row>
        </form>


        <abp-table striped-rows="true" id="AbpAuditLogssTable">
            <thead>
                <tr>
                    <th width="40">@L["Http"]</th>
                    <th width="40">@L["Method"]</th>
                    <th>@L["UserName"]</th>
                    <th>@L["ExecutionDuration"]</th>
                    <th>@L["ClientIpAddress"]</th>
                    <th>@L["Url"]</th>
                    <th>@L["ExecutionTime"]</th>
                </tr>
            </thead>
        </abp-table>
    </abp-card-body>
</abp-card>

6、增加index.js代碼:

   $(function () {

        var l = abp.localization.getResource('AuditManagement');
        var _auditLogService = xxxnamespace.auditLog;
        var _$auditLogFilterForm = $('#AuditLogFilterForm');
        var _$auditLogsTable = $('#AbpAuditLogssTable');
        var _selectedDateRangeAuditLog = {
            startDate: moment().startOf('day'),
            endDate: moment().endOf('day')
        };

        var createDateRangePickerOptions = function (extraOptions) {
            extraOptions = extraOptions ||
            {
                allowFutureDate: false
            };

            var options = {
                locale: {
                    format: 'L',
                    applyLabel: l('Apply'),
                    cancelLabel: l('Cancel'),
                    customRangeLabel: l('CustomRange')
                },
                min: moment('2015-05-01'),
                minDate: moment('2015-05-01'),
                opens: 'left',
                ranges: {}
            };

            if (!extraOptions.allowFutureDate) {
                options.max = moment();
                options.maxDate = moment();
            }

            options.ranges[l('Today')] = [moment().startOf('day'), moment().endOf('day')];
            options.ranges[l('Yesterday')] = [moment().subtract(1, 'days').startOf('day'), moment().subtract(1, 'days').endOf('day')];
            options.ranges[l('Last7Days')] = [moment().subtract(6, 'days').startOf('day'), moment().endOf('day')];
            options.ranges[l('Last30Days')] = [moment().subtract(29, 'days').startOf('day'), moment().endOf('day')];
            options.ranges[l('ThisMonth')] = [moment().startOf('month'), moment().endOf('month')];
            options.ranges[l('LastMonth')] = [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')];

            return options;
        };

        _$auditLogFilterForm.find('input.date-range-picker').daterangepicker(
            $.extend(true, createDateRangePickerOptions(), _selectedDateRangeAuditLog),
            function (start, end) {
                _selectedDateRangeAuditLog.startDate = start.format('YYYY-MM-DDT00:00:00Z');
                _selectedDateRangeAuditLog.endDate = end.format('YYYY-MM-DDT23:59:59.999Z');

                getAuditLogs();
            });




        var dataTable = _$auditLogsTable.DataTable(abp.libs.datatables.normalizeConfiguration({
            processing: true,
            serverSide: true,
            paging: true,
            searching: false,
            autoWidth: false,
            scrollCollapse: true,
            order: [[1, "asc"]],

            ajax: abp.libs.datatables.createAjax(_auditLogService.getList,
                function () { return createAuditLogRequestParams() }),
            columnDefs: [

                {
                    data: "httpStatusCode"
                },
                {
                    data: "httpMethod"
                },
                { data: "userName" },
                { data: "executionDuration" },
                { data: "clientIpAddress" },
                { data: "url" },
                {

                    data: "executionTime",
                    render: function (executionTime) {
                        return moment(executionTime).format('YYYY-MM-DD HH:mm:ss');
                    }
                }
            ]
        }));

        function createAuditLogRequestParams() {
            var prms = {};
            _$auditLogFilterForm.serializeArray().map(function (x) {

                prms[abp.utils.toCamelCase(x.name.substring(6))] = x.value;
            });
            return $.extend(prms, _selectedDateRangeAuditLog);
        }
        function getAuditLogs() {
            dataTable.ajax.reload();
        }

        $('#RefreshAuditLogsButton').click(function (e) {
            e.preventDefault();
            getAuditLogs();
        });
    });

7、在xxMenuContributor.cs中添加菜單:

  context.Menu.Items.Add(new ApplicationMenuItem("Stock.Audit", l["Menu:Audit"], "/Audits"));

基本的代碼就是這些,當(dāng)然,如果要真正做成一個(gè)實(shí)用的日志功能,最好是把它做成一個(gè)模塊,然后添加到你的新項(xiàng)目中。還要有查詢功能等等。


image.png
image.png
image.png
image.png

上面的圖片是我最終做出來(lái)的日志界面,還有待完善。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,936評(píng)論 6 535
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,744評(píng)論 3 421
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 176,879評(píng)論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 63,181評(píng)論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,935評(píng)論 6 410
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 55,325評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,384評(píng)論 3 443
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 42,534評(píng)論 0 289
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,084評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,892評(píng)論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,067評(píng)論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,623評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,322評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 34,735評(píng)論 0 27
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 35,990評(píng)論 1 289
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,800評(píng)論 3 395
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,084評(píng)論 2 375