Buscando en la red encontré una base en Code Project para una aplicación de escritorio, teniendo el siguiente código en C#:
intentando aplicarlo en ASP.NET surgen 3 detalles.
Resultado:
Al final para lograr el monitoreo de los log lo que realicé fue lo siguiente:
1.- Creé una función javascript que inicializa un timer dejándolo a 30 segundos.
function StartTimer() {
setTimeout('DoPostBack()', 30000);
}
2.- Agregué un botón con un clientID=Button1 para forzar un PostBack, con el click de dicho botón desde javascript.
3.- La función encargada de forzar el click, forza el PostBack y hay un request para poder utilizar el response, después vuelve a asignar el timer a la misma función haciéndola recursiva.
function DoPostBack() {
var btn = document.getElementById('Button1');
if (btn != null)
{
btn.click();
StartTimer();
}
else
{
alert('No encontre el botón.');
}
}
4.- Se creo una función javascript donde se logró el scrollTop, llamandola desde el OnLoad del body en nuestro html, con una llamada a la función timer para iniciar la recursión.
function scrollme() {
dh = document.body.scrollHeight
ch = document.body.clientHeight
if (dh > ch) {
moveme = dh - ch
window.scrollTo(0, moveme)
}
StartTimer();
}
5.- En el metodo Page_Load quedo de la siguiente manera:
If Not Page.IsPostBack Then
'Inicio de Página
If Not Request.QueryString("FPath") Is Nothing And Request.QueryString("Offset") Is Nothing Then
Try
FPath = Request.QueryString("FPath").ToString.Trim
Dim reader As New StreamReader(New FileStream(FPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Dim line As String
lastMaxOffset = reader.BaseStream.Length
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = reader.ReadLine()
Response.Write("<font color='" & AppSettings("ForeColor") & "'>" & line & "</font><br/>")
Loop Until line Is Nothing
'Actualizar Url con Offset Actual para uso posterior con PostBack
Response.Redirect(Request.Url.AbsolutePath & "?FPath=" & FPath & "&Offset=" & lastMaxOffset)
Catch ex As Exception
Response.Write("<font color='red'>" & ex.ToString & "</font><br/>")
End Try
Else
'Cargar info de Offset en adelante
Try
FPath = Request.QueryString("FPath").ToString.Trim
Dim reader As New StreamReader(New FileStream(FPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Dim line As String
lastMaxOffset = Request.QueryString("Offset").ToString.Trim
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = reader.ReadLine()
Response.Write("<font color='" & AppSettings("ForeColor") & "'>" & line & "</font><br/>")
Loop Until line Is Nothing
Catch ex As Exception
Response.Write("<font color='red'>" & ex.ToString & "</font><br/>")
End Try
End If
ElseIf Not Request.QueryString("FPath") Is Nothing And Not Request.QueryString("Offset") Is Nothing Then
Dim FilePathParam As String = Request.QueryString("FPath").ToString.Trim
Dim OffsetParam As Long = FPath = Request.QueryString("Offset").ToString.Trim
Dim reader As New StreamReader(New FileStream(FilePathParam, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
If reader.BaseStream.Length > OffsetParam Then
'seek to the last max offset
reader.BaseStream.Seek(OffsetParam, SeekOrigin.Begin)
'read out of the file until the EOF
While reader.BaseStream.Length > reader.BaseStream.Position
Response.Write("<font color='" & AppSettings("ForeColor") & "'>" & reader.ReadLine & "</font><br/>")
End While
'update the last max offset
OffsetParam = reader.BaseStream.Position
End If
End If
End Sub
Podemos observar que:
° Al iniciar la página trae solo el parámetro FPath que contiene la ruta del log entrando en la primer condicionante If donde se carga el contenido actual del archivo y hace una redirección con Response.Redirect hacia la misma página,agregando el parámetro Offset con la posición donde se quedo.
° Despues de la redirección no siendo aun un PostBack entra en el else donde carga el contenido nuevo en caso de existir partiendo del Offset.
° A los 30 seg. asignados se dispara el click del botón siendo este un Postback accediendo a la parte del codigo en el ElseIf del primer If donde carga los cambios existentes partiendo del Offset.
Con esto obtuve una aplicación Web tipo Unix Tail
También hice posible que el color tanto de fondo como de la fuente quedaran configurables desde el Web.Config como se observa en el Response.Write utilizando Appsettings
Espero sea de utilidad.
using ( StreamReader reader = new StreamReader(new FileStream(fileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) )
{
//start at the end of the file
long lastMaxOffset = reader.BaseStream.Length;
while ( true )
{
System.Threading.Thread.Sleep(100);
//if the file size has not changed, idle
if ( reader.BaseStream.Length == lastMaxOffset )
continue;
//seek to the last max offset
reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin);
//read out of the file until the EOF
string line = "";
while ( (line = reader.ReadLine()) != null )
Console.WriteLine(line);
//update the last max offset
lastMaxOffset = reader.BaseStream.Position;
}
}
intentando aplicarlo en ASP.NET surgen 3 detalles.
- No es posible utilizar un loop infinito (While true) ya que la manera de escribir en la página Web es utilizando Response.Write o ejecutando una función javascript del lado del cliente desde el lado del servidor donde dicha función realice un document.write, Response requiere un Request el cual se recibe en el Load y la función javascript requiere un postback para que se actualice la info insertada, se tiene la restricción de cargar el log en el evento Load y si se utiliza el loop infinito en el Load la pagina nunca cargaria.
- Se requiere un timer para que este cargando los cambios realizados del archivo y para que los cambios sean mostrados es necesario un Request o Postback.
- Una vez logrado lo anterior es necesario que el scroll este siempre al final donde las últimas modificaciones se encuentran.
Resultado:
Al final para lograr el monitoreo de los log lo que realicé fue lo siguiente:
1.- Creé una función javascript que inicializa un timer dejándolo a 30 segundos.
function StartTimer() {
setTimeout('DoPostBack()', 30000);
}
2.- Agregué un botón con un clientID=Button1 para forzar un PostBack, con el click de dicho botón desde javascript.
3.- La función encargada de forzar el click, forza el PostBack y hay un request para poder utilizar el response, después vuelve a asignar el timer a la misma función haciéndola recursiva.
function DoPostBack() {
var btn = document.getElementById('Button1');
if (btn != null)
{
btn.click();
StartTimer();
}
else
{
alert('No encontre el botón.');
}
}
4.- Se creo una función javascript donde se logró el scrollTop, llamandola desde el OnLoad del body en nuestro html, con una llamada a la función timer para iniciar la recursión.
function scrollme() {
dh = document.body.scrollHeight
ch = document.body.clientHeight
if (dh > ch) {
moveme = dh - ch
window.scrollTo(0, moveme)
}
StartTimer();
}
5.- En el metodo Page_Load quedo de la siguiente manera:
If Not Page.IsPostBack Then
'Inicio de Página
If Not Request.QueryString("FPath") Is Nothing And Request.QueryString("Offset") Is Nothing Then
Try
FPath = Request.QueryString("FPath").ToString.Trim
Dim reader As New StreamReader(New FileStream(FPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Dim line As String
lastMaxOffset = reader.BaseStream.Length
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = reader.ReadLine()
Response.Write("<font color='" & AppSettings("ForeColor") & "'>" & line & "</font><br/>")
Loop Until line Is Nothing
'Actualizar Url con Offset Actual para uso posterior con PostBack
Response.Redirect(Request.Url.AbsolutePath & "?FPath=" & FPath & "&Offset=" & lastMaxOffset)
Catch ex As Exception
Response.Write("<font color='red'>" & ex.ToString & "</font><br/>")
End Try
Else
'Cargar info de Offset en adelante
Try
FPath = Request.QueryString("FPath").ToString.Trim
Dim reader As New StreamReader(New FileStream(FPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Dim line As String
lastMaxOffset = Request.QueryString("Offset").ToString.Trim
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = reader.ReadLine()
Response.Write("<font color='" & AppSettings("ForeColor") & "'>" & line & "</font><br/>")
Loop Until line Is Nothing
Catch ex As Exception
Response.Write("<font color='red'>" & ex.ToString & "</font><br/>")
End Try
End If
ElseIf Not Request.QueryString("FPath") Is Nothing And Not Request.QueryString("Offset") Is Nothing Then
Dim FilePathParam As String = Request.QueryString("FPath").ToString.Trim
Dim OffsetParam As Long = FPath = Request.QueryString("Offset").ToString.Trim
Dim reader As New StreamReader(New FileStream(FilePathParam, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
If reader.BaseStream.Length > OffsetParam Then
'seek to the last max offset
reader.BaseStream.Seek(OffsetParam, SeekOrigin.Begin)
'read out of the file until the EOF
While reader.BaseStream.Length > reader.BaseStream.Position
Response.Write("<font color='" & AppSettings("ForeColor") & "'>" & reader.ReadLine & "</font><br/>")
End While
'update the last max offset
OffsetParam = reader.BaseStream.Position
End If
End If
End Sub
Podemos observar que:
° Al iniciar la página trae solo el parámetro FPath que contiene la ruta del log entrando en la primer condicionante If donde se carga el contenido actual del archivo y hace una redirección con Response.Redirect hacia la misma página,agregando el parámetro Offset con la posición donde se quedo.
° Despues de la redirección no siendo aun un PostBack entra en el else donde carga el contenido nuevo en caso de existir partiendo del Offset.
° A los 30 seg. asignados se dispara el click del botón siendo este un Postback accediendo a la parte del codigo en el ElseIf del primer If donde carga los cambios existentes partiendo del Offset.
Con esto obtuve una aplicación Web tipo Unix Tail
También hice posible que el color tanto de fondo como de la fuente quedaran configurables desde el Web.Config como se observa en el Response.Write utilizando Appsettings
Espero sea de utilidad.
No hay comentarios:
Publicar un comentario