简体   繁体   English

在运行时读取日志文件并在客户端逐行显示

[英]Reading log file during run time and displaying line by line at client side

A log file (log4net) is getting created by clicking on a button. 单击一个按钮可创建一个日志文件(log4net)。 I need to read that file during run time only and display the result line by line to the client end, mostly like a progress bar. 我只需要在运行时读取该文件,并将结果逐行显示到客户端,就像进度条一样。 How to achieve this? 如何实现呢? I am using asp.net 4.5, c# 我正在使用asp.net 4.5,C#

I tried calling some java script function from server side, but obviously it is showing only final result. 我尝试从服务器端调用一些Java脚本函数,但显然它仅显示最终结果。 It has to be simultaneous process. 它必须是同步过程。

You can add a memoryappender to your logconfiguration, this will also log to the memory so you dont need to read the file in the same time. 您可以将memoryappender添加到日志配置中,这还将记录到内存中,因此您无需同时读取文件。 Configuration can be done Via the xml config or via code: 可以通过xml config或通过代码完成配置:

/// <summary>
/// Attaches a memory appender
/// </summary>
/// <param name="log"></param>
public static MemoryAppender AttachMemoryAppender(this ILog log)
{
    var memoryAppender = new log4net.Appender.MemoryAppender
    {
        //  *** AppenderSkeleton ***
        Name = log.GetLogger().Name,
        Threshold = log.GetLogger().Repository.LevelMap["DEBUG"],

        //  *** MemoryAppender ***
        Fix = FixFlags.All
    };

    var layout = new log4net.Layout.PatternLayout
    {
        ConversionPattern = "%date - %-5level - %message%newline"
    };

    layout.ActivateOptions();
    memoryAppender.Layout = layout;
    memoryAppender.ActivateOptions();

    log.GetLogger().AddAppender(memoryAppender);

    return memoryAppender;
}

to get the logs use: 获取日志使用:

var events = memoryAppender.GetEvents();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM