【问题标题】:Error While Read Windows Log Event in mvc 6在 mvc 6 中读取 Windows 日志事件时出错
【发布时间】:2016-05-13 07:17:46
【问题描述】:

我正在尝试创建一个 Web 应用程序来读取所有 Windows 事件日志并显示到网页中。我在 asp.net mvc 5 中尝试过它的工作,但是当我尝试在 asp.net mvc 6 应用程序中执行它时显示错误。

控制器

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
using WebApplication1.Models.PaginationExample;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
        EventLog eventLog = new EventLog("Application", "nb-ahja1", "EventLoggingApp");

        var model = new List<Eventlogg>();
        foreach (EventLogEntry log in eventLog.Entries)
        {
            var demo = new Eventlogg();
            demo.Source = log.Source;
            demo.Id = log.EventID;
            demo.Level = log.EntryType.ToString();
            demo.Date = log.TimeGenerated;
            demo.Category = log.Category;
            model.Add(demo);
        }
        return View(model.OrderByDescending(x => x.Date).ToList());
    }


    public ActionResult About()
    {


        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}
}

型号

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class Eventlogg
    {
        public string Source { get; set; }
        public int Id { get; set; }
        public string Level { get; set; }
        public DateTime Date { get; set; }
        public string Category { get; set; }

    }
}

查看

@model List<WebApplication1.Models.Eventlogg>
@{
    ViewBag.Title = "Home Page";
}

<div class="container" >

    <table class="table table-hover" id="myTable">
        <tr>
            <td class="col-md-3"><b>Level</b></td>
        <td class="col-md-3"><b>Date</b></td>
        <td class="col-md-3"><b>Source</b></td>
        <td class="col-md-3"><b>Id</b></td>
        <td class="col-md-3"><b>Category</b></td>
        </tr>
        
@foreach (var item in Model)
{
        <tr>
            <td class="col-md-3">@item.Level</td>
            <td class="col-md-3">@item.Date</td>
            <td class="col-md-3">@item.Source</td>
            <td class="col-md-3">@item.Id</td>
            <td class="col-md-3">@item.Category</td>
        </tr>
}
    </table>

    </div>

错误

严重性代码描述项目文件行抑制状态 错误 CS0246 找不到类型或命名空间名称“EventLog”(您是否缺少 using 指令或程序集引用?) PocDashboard.DNX Core 5.0 C:\Users\ahja\Documents\Visual Studio 2015\Projects\AteaPoCDashboard\src \PocDashboard\Controllers\HomeController.cs 19 活动

【问题讨论】:

    标签: c# asp.net-core-mvc


    【解决方案1】:

    基本上,您缺少需要添加到项目中的程序集引用。

    'EventLog' 是 system.diagnostics 命名空间的一部分,它是 System.dll 程序集的一部分,请确保在您的项目中引用了此程序集。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 2018-03-15
      • 2013-06-04
      相关资源
      最近更新 更多