如何实现IEnumerable<T>和IEnumerator接口,一个ASP.NET MVC日志模型的实现
2011-08-28 15:58
846 查看
namespace MyMvcApp.Models
{
public class LogsModel:IEnumerable<IDictionary<string,string>>,IEnumerator<IDictionary<string,string>>
{
private TextReader reader;
private IDictionary<string, string> currentLog;
private string logFileName { get; set; }
public int LogCount { get; set; }
public LogsModel(string logFileName = @"logs\sys.log")
{
currentLog = new Dictionary<string, string>();
this.logFileName = AppDomain.CurrentDomain.BaseDirectory + logFileName;
InitLogReader(this.logFileName);
}
public void CloseLogFile()
{
if (reader != null)
{
reader.Close();
reader = null;
}
}
public long LogByteSize
{
get
{
FileInfo fi = new FileInfo(logFileName);
return fi.Length;
}
}
public void ClearLog()
{
CloseLogFile();
TextWriter writer = new StreamWriter(logFileName,false);
writer.Write("");
writer.Close();
}
private void InitLogReader(string logFileName)
{
try
{
reader = new StreamReader(logFileName);
}
catch (FileNotFoundException e)
{
File.CreateText(logFileName);
throw (e);
}
finally
{
reader = new StreamReader(logFileName);
}
}
public IDictionary<string, string> Current
{
get
{
string line = reader.ReadLine();
try
{
if (!string.IsNullOrEmpty(line))
{
foreach (string pair in line.Split(';'))
{
string[] keyValue = pair.Split(':');
string key = keyValue[0];
string value = keyValue[1];
if (keyValue.Length > 2)
{
value += ":" + keyValue[2];
}
currentLog.Add(key, value);
}
}
return currentLog;
}
catch (Exception e)
{
throw e;
}
}
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public void Reset()
{
CloseLogFile();
LogCount = 0;
InitLogReader(logFileName);
}
public bool MoveNext()
{
currentLog.Clear();
if (reader.Peek() > -1)
{
LogCount++;
return true;
}
CloseLogFile();
return false;
}
void IEnumerator.Reset()
{
Reset();
}
bool IEnumerator.MoveNext()
{
return MoveNext();
}
public void Dispose()
{
CloseLogFile();
}
public IEnumerator<IDictionary<string,string>> GetEnumerator()
{
return this;
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)this;
}
}
}
{
public class LogsModel:IEnumerable<IDictionary<string,string>>,IEnumerator<IDictionary<string,string>>
{
private TextReader reader;
private IDictionary<string, string> currentLog;
private string logFileName { get; set; }
public int LogCount { get; set; }
public LogsModel(string logFileName = @"logs\sys.log")
{
currentLog = new Dictionary<string, string>();
this.logFileName = AppDomain.CurrentDomain.BaseDirectory + logFileName;
InitLogReader(this.logFileName);
}
public void CloseLogFile()
{
if (reader != null)
{
reader.Close();
reader = null;
}
}
public long LogByteSize
{
get
{
FileInfo fi = new FileInfo(logFileName);
return fi.Length;
}
}
public void ClearLog()
{
CloseLogFile();
TextWriter writer = new StreamWriter(logFileName,false);
writer.Write("");
writer.Close();
}
private void InitLogReader(string logFileName)
{
try
{
reader = new StreamReader(logFileName);
}
catch (FileNotFoundException e)
{
File.CreateText(logFileName);
throw (e);
}
finally
{
reader = new StreamReader(logFileName);
}
}
public IDictionary<string, string> Current
{
get
{
string line = reader.ReadLine();
try
{
if (!string.IsNullOrEmpty(line))
{
foreach (string pair in line.Split(';'))
{
string[] keyValue = pair.Split(':');
string key = keyValue[0];
string value = keyValue[1];
if (keyValue.Length > 2)
{
value += ":" + keyValue[2];
}
currentLog.Add(key, value);
}
}
return currentLog;
}
catch (Exception e)
{
throw e;
}
}
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public void Reset()
{
CloseLogFile();
LogCount = 0;
InitLogReader(logFileName);
}
public bool MoveNext()
{
currentLog.Clear();
if (reader.Peek() > -1)
{
LogCount++;
return true;
}
CloseLogFile();
return false;
}
void IEnumerator.Reset()
{
Reset();
}
bool IEnumerator.MoveNext()
{
return MoveNext();
}
public void Dispose()
{
CloseLogFile();
}
public IEnumerator<IDictionary<string,string>> GetEnumerator()
{
return this;
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)this;
}
}
}
相关文章推荐
- asp.net mvc 如何将controller 里一个action 返回值为list<>的值输出到view
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- Nop-Fluent+AutoFAC模式在ASP.NET MVC中的应用场景<四>
- asp.net mvc <tr>变&lt;tr/&gt; @Html.Raw 作用
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
- ASP.NET MVC 中的IResolver<T> 接口
- 如何使用<Beginning ASP.NET 3.5 in C# and VB>书中的示例代码
- ASP.NET MVC (Razor)开发 <<周报与绩效考核系统>> 总结与分享
- New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)
- 如何在ASP.NET MVC中实现提交若干个某模型的数据(某Model的List或ICollection,大小不定)
- ASP.NET MVC:如何实现一个自己的ModelBinder?
- <asp.net> 按一次button, 添加一个textbox
- Nop-ASP.NET MVC Area概念的应用Admin后台项目<三>
- ASP.NET MVC 3: Razor’s @: and <text> syntax
- [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(5)(IEnumerable<>补充)
- Nop-Fluent+AutoFAC模式在ASP.NET MVC中的应用场景<四>
- <转>ASP.NET MVC HtmlHelper用法大全
- session + List<T> 实现购物车(asp.net mvc)
- Note on <Pro ASP.NET MVC 4> - 01
- 写一个MyList<T>的类,内部用T[]实现(不能使用系统的List<T>类),需要实现的接口如下: